#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/8
# @Author :CJX
# @File   :main.py
import os
import sys
from collections import deque
from typing import List, Union, Any
import RPi.GPIO as GPIO
import can
import time
import numpy as np
from smbus import SMBus
from Injection_valve_runze import injection_port  # 切换阀切换
from Injection_valve_runze import insert_database  # 查询切换阀位置
from Injection_valve_runze import insert_error_num  # 记录对应步骤，方便跳转
from Injection_valve_runze import step_num  # 用来查询跳转到第几步
from Injection_valve_runze import error_val  # 用来查询是否跳转，0为不跳转
from dox_enquiry import dox_command  # 写入do点
from PH_controller import PH_num  # PH仪表PH值查询
from PH_controller import PH_enter_calibration  # PH仪表进入校准
from PH_controller import PH_controller  # PH仪表校准确认
from PH_controller import PH_start  # 上电执行一次用于清楚PH仪表的时间显示
from threading import Thread
import threading
import pymysql

import binascii
import importlib
import codecs
import serial
# DHT20
from DHT22 import *
from DHT485 import *

importlib.reload(sys)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
ser = serial.Serial('/dev/ttySC0', 9600)


def DHT11():
    time.sleep(2)
    serial_val_modify('click give_time_pi,1')  # 为了获取屏幕的时间
    DHT11_error_wai = 0
    DHT11_error_nei = 0
    if di_location[4:5][0] == "1":
        serial_val_modify("homepage.state_sw.val=0")
    if di_location[5:6][0] == "1":
        serial_val_modify("homepage.state_sw.val=1")
    while 1:
        global err_inquiry_13, err_inquiry_14_16
        global DHT11_state_13, DHT11_state_14, DHT11_state_15, DHT11_state_16, DHT11_state_17
        global tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan
        DHT20_num = DHT22_485(1)
        time.sleep(5)
        DHT20_num_1 = DHT22_485(2)
        time.sleep(5)
        temperature = DHT20_num[0]
        humidity = DHT20_num[1]
        temperature_1 = DHT20_num_1[0]
        humidity_1 = DHT20_num_1[1]
        if temperature < 100 and humidity < 100 and temperature_1 < 100 and humidity_1 < 100:
            print("外部温度是： ", temperature, "外部湿度是： ", humidity)
            DHT11_error_wai = 0
            DHT11_error_nei = 0
            serial_val_modify("homepage.tem.val=%s" % temperature)
            serial_val_modify("homepage.hum.val=%s" % humidity)
            time.sleep(2)
            serial_val_modify("tem_2.tem.val=%s" % temperature_1)
            serial_val_modify("tem_2.hum.val=%s" % humidity_1)
            # DHT20_num_1 = DHT22_485(1)
            print("内部温度是： ", temperature_1, "内部湿度是： ", humidity_1)
            if temperature_1 >= tem_up_fan or humidity_1 >= hum_up_fan:
                DHT11_state_13 = [13]
                # dox_command([13], err_inquiry_13)
                # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                # serial_val_modify("warningpage.data0.insert(\"%s^环境温度高^无\")" % time1)  # 存入故障信息
                print("开启")
            elif temperature_1 <= tem_down_fan and humidity_1 <= hum_down_fan:
                DHT11_state_13 = [0]
                # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                # serial_val_modify("warningpage.data0.insert(\"%s^环境温度正常^无\")" % time1)  # 存入故障信息
                # dox_command([0], err_inquiry_13)
            time.sleep(5)
            # if humidity >= HumH:
            #     HumH_t = 1
            #     DHT11_state_13 = [13]
            #     # dox_command([13], err_inquiry_13)
            #     # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            #     # serial_val_modify("warningpage.data0.insert(\"%s^环境湿度高^无\")" % time1)  # 存入故障信息
            # elif humidity <= (HumH - 2):
            #     HumH_t = 0
            #     DHT11_state_13 = [0]
            #     # dox_command([0], err_inquiry_13)
            #     # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            #     # serial_val_modify("warningpage.data0.insert(\"%s^环境湿度正常^无\")" % time1)  # 存入故障信息
            # time.sleep(5)
            if temperature < tem_up_heat:
                DHT11_state_14 = [14]
                DHT11_state_15 = [15]
                DHT11_state_16 = [16]
                # dox_command([14, 15, 16], err_inquiry_14_16)
            elif temperature > tem_down_heat:
                DHT11_state_14 = [0]
                DHT11_state_15 = [0]
                DHT11_state_16 = [0]
                DHT11_state_17 = [0]
                # dox_command([0], err_inquiry_14_16)
            if (di_location[0:1][0] == "1" and temperature < tem_up_heat) or \
                    (maintenance_status == 1 and temperature < tem_up_heat):
                DHT11_state_17 = [17]
            if maintenance_status == 0 and di_location[0:1][0] == "0":
                DHT11_state_17 = [0]
            # time.sleep(50)
        if temperature > 100:
            DHT11_error_wai = DHT11_error_wai + 1
        if DHT11_error_wai >= 5:
            DHT11_error_wai = 0
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^外部温感故障\")" % time1)  # 存入故障信息
        if temperature_1 > 100:
            DHT11_error_nei = DHT11_error_nei + 1
        if DHT11_error_nei >= 5:
            DHT11_error_nei = 0
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^内部温感故障\")" % time1)  # 存入故障信息
        time.sleep(105)


def water_level_monitoring():
    global i2c40, i2c41, i2c42, i2c43
    global YW40L, YW41L, YW42L, YW43L, YW40H, YW41H, YW42H, YW43H
    global i2c40_t, i2c41_t, i2c42_t, i2c43_t, err_inquiry_22, err_inquiry_23, err_inquiry_24
    global water_level_22, water_level_23, water_level_24, mode, maintenance_status
    time.sleep(2)
    # 当设置为PUD_OFF时表示没有上拉电阻和下拉电阻，当设置为PUD_DOWN 时为下拉电阻，设置为PUD_UP是为上拉电阻。
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 清洗液低液位
    GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 清洗液高液位
    GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 保护液低液位
    GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 保护液高液位
    GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液6低液位
    GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液6高液位
    GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液4低液位
    GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # 缓冲液4高液位
    time.sleep(0.5)
    if GPIO.input(17) == 1:
        GPIO_t_17 = 0
    else:
        GPIO_t_17 = 1

    if GPIO.input(4) == 1:
        GPIO_t_4 = 0
    else:
        GPIO_t_4 = 1

    if GPIO.input(22) == 1:
        GPIO_t_22 = 0
    else:
        GPIO_t_22 = 1

    if GPIO.input(27) == 1:
        GPIO_t_27 = 0
    else:
        GPIO_t_27 = 1

    if GPIO.input(26) == 1:
        GPIO_t_26 = 0
    else:
        GPIO_t_26 = 1

    if GPIO.input(13) == 1:
        GPIO_t_13 = 0
    else:
        GPIO_t_13 = 1

    if GPIO.input(6) == 1:
        GPIO_t_6 = 0
    else:
        GPIO_t_6 = 1

    if GPIO.input(5) == 1:
        GPIO_t_5 = 0
    else:
        GPIO_t_5 = 1
    while 1:
        time.sleep(0.5)

        if GPIO.input(17) == 1 and GPIO.input(4) == 0:
            i2c40 = 100
            # print("清洗液中液位")
        elif GPIO.input(17) == 1 and GPIO.input(4) == 1:
            i2c40 = 400
            # print("清洗液高液位")
        elif GPIO.input(17) == 0 and GPIO.input(4) == 0:
            i2c40 = 0
            # print("清洗液低液位")

        if GPIO.input(17) == 1 and GPIO_t_17 == 0:
            serial_val_modify("drugpage.clean_low.val=100")
            GPIO_t_17 = 1
        elif GPIO.input(17) == 0 and GPIO_t_17 == 1:
            serial_val_modify("drugpage.clean_low.val=0")
            GPIO_t_17 = 0

        if GPIO.input(4) == 1 and GPIO_t_4 == 0:
            serial_val_modify("drugpage.clean_high.val=100")
            GPIO_t_4 = 1
        elif GPIO.input(4) == 0 and GPIO_t_4 == 1:
            serial_val_modify("drugpage.clean_high.val=0")
            GPIO_t_4 = 0

        if GPIO.input(22) == 1 and GPIO.input(27) == 0:
            i2c41 = 100
            # print("保护液中液位")
        elif GPIO.input(22) == 1 and GPIO.input(27) == 1:
            i2c41 = 400
            # print("保护液高液位")
        elif GPIO.input(22) == 0 and GPIO.input(27) == 0:
            i2c41 = 0
            # print("保护液低液位")

        if GPIO.input(22) == 1 and GPIO_t_22 == 0:
            serial_val_modify("drugpage.pro_low.val=100")
            GPIO_t_22 = 1
        elif GPIO.input(22) == 0 and GPIO_t_22 == 1:
            serial_val_modify("drugpage.pro_low.val=0")
            GPIO_t_22 = 0

        if GPIO.input(27) == 1 and GPIO_t_27 == 0:
            serial_val_modify("drugpage.pro_high.val=100")
            GPIO_t_27 = 1
        elif GPIO.input(27) == 0 and GPIO_t_27 == 1:
            serial_val_modify("drugpage.pro_high.val=0")
            GPIO_t_27 = 0

        if GPIO.input(26) == 1 and GPIO.input(13) == 0:
            i2c43 = 100
            # print("缓冲液6中液位")
        elif GPIO.input(26) == 0 and GPIO.input(13) == 0:
            i2c43 = 0
            # print("缓冲液6低液位")
        elif GPIO.input(13) == 1 and GPIO.input(26) == 1:
            i2c43 = 400
            # print("缓冲液6高液位")

        if GPIO.input(26) == 1 and GPIO_t_26 == 0:
            serial_val_modify("drugpage.add_6_low.val=100")
            GPIO_t_26 = 1
        elif GPIO.input(26) == 0 and GPIO_t_26 == 1:
            serial_val_modify("drugpage.add_6_low.val=0")
            GPIO_t_26 = 0

        if GPIO.input(13) == 1 and GPIO_t_13 == 0:
            serial_val_modify("drugpage.add_6_high.val=100")
            GPIO_t_13 = 1
        elif GPIO.input(13) == 0 and GPIO_t_13 == 1:
            serial_val_modify("drugpage.add_6_high.val=0")
            GPIO_t_13 = 0

        if GPIO.input(6) == 1 and GPIO.input(5) == 0:
            i2c42 = 100
            # print("缓冲液4中液位")
        elif GPIO.input(6) == 0 and GPIO.input(5) == 0:
            i2c42 = 0
            # print("缓冲液4低液位")
        elif GPIO.input(6) == 1 and GPIO.input(5) == 1:
            i2c42 = 400
            # print("缓冲液4高液位")

        if GPIO.input(6) == 1 and GPIO_t_6 == 0:
            serial_val_modify("drugpage.add_4_low.val=100")
            GPIO_t_6 = 1
        elif GPIO.input(6) == 0 and GPIO_t_6 == 1:
            serial_val_modify("drugpage.add_4_low.val=0")
            GPIO_t_6 = 0

        if GPIO.input(5) == 1 and GPIO_t_5 == 0:
            serial_val_modify("drugpage.add_4_high.val=100")
            GPIO_t_5 = 1
        elif GPIO.input(5) == 0 and GPIO_t_5 == 1:
            serial_val_modify("drugpage.add_4_high.val=0")
            GPIO_t_5 = 0
        # iic = SMBus(1)  # 创建接口
        # i2c40_t = 0
        # i2c41_t = 0
        # i2c42_t = 0
        # i2c43_t = 0
        # while 1:
        #     list1, list2, list3, list4 = [], [], [], []
        #     for i in range(5):  # 初始化，预读n组数据
        #         data1 = iic.read_word_data(0x40, 0x00)
        #         list1.append(data1)
        #         data2 = iic.read_word_data(0x41, 0x00)
        #         list2.append(data2)
        #         data3 = iic.read_word_data(0x42, 0x00)
        #         list3.append(data3)
        #         data4 = iic.read_word_data(0x43, 0x00)
        #         list4.append(data4)
        #         time.sleep(0.1)
        #
        #     average1 = round(sum(list1) / len(list1))  # 平均值四舍五入
        #     average2 = round(sum(list2) / len(list2))
        #     average3 = round(sum(list3) / len(list3))
        #     average4 = round(sum(list4) / len(list4))
        #     i2c40 = average1
        #     i2c41 = average2
        #     i2c42 = average3
        #     i2c43 = average4
        if i2c40 <= YW40L and i2c40_t == 0:
            if mode != 1 and maintenance_status != 1:
                i2c40_t = 1
                # # serial_val_modify("page0.now_position.txt=\"清洗液液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^清洗液液位过低\")" % time1)  # 存入故障信息
                dox_command([22], err_inquiry_22)
                water_level_22 = [22]
            else:
                i2c40_t = 1
                # # serial_val_modify("page0.now_position.txt=\"清洗液液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^清洗液液位过低\")" % time1)  # 存入故障信息
                water_level_22 = [22]
        if i2c40 > YW40L and i2c40_t == 1:
            i2c40_t = 0
            # # serial_val_modify("page0.now_position.txt=\"清洗液液位正常\"")
            water_level_22 = [0]
            dox_command([0], err_inquiry_22)

        if i2c41 <= YW41L and i2c41_t == 0:
            if mode != 1 and maintenance_status != 1:
                i2c41_t = 1
                # # serial_val_modify("page0.now_position.txt=\"保护液液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^保护液液位过低\")" % time1)  # 存入故障信息
                dox_command([23], err_inquiry_23)
                water_level_23 = [23]
            else:
                i2c41_t = 1
                # # serial_val_modify("page0.now_position.txt=\"保护液液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^保护液液位过低\")" % time1)  # 存入故障信息
                water_level_23 = [23]
        if i2c41 > YW41L and i2c41_t == 1:
            water_level_23 = [0]
            time.sleep(1)
            dox_command([0], err_inquiry_23)
            i2c41_t = 0
            # # serial_val_modify("page0.now_position.txt=\"保护液液位正常\"")

        if i2c42 <= YW42L and i2c42_t == 0:
            if mode != 1 and maintenance_status != 1:
                i2c42_t = 1
                # # serial_val_modify("page0.now_position.txt=\"缓冲液4液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^缓冲液4液位过低\")" % time1)  # 存入故障信息
                dox_command([24], err_inquiry_24)
                water_level_24 = [24]
            else:
                i2c42_t = 1
                # # serial_val_modify("page0.now_position.txt=\"缓冲液4液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^缓冲液4液位过低\")" % time1)  # 存入故障信息
                water_level_24 = [24]
        if i2c42 > YW42L and i2c42_t == 1 and i2c43 > YW43L:
            water_level_24 = [0]
            time.sleep(1)
            dox_command([0], err_inquiry_24)
            i2c42_t = 0
            # # serial_val_modify("page0.now_position.txt=\"缓冲液4液位正常\"")

        if i2c43 <= YW43L and i2c43_t == 0:
            if mode != 1 and maintenance_status != 1:  # 在自动状态
                i2c43_t = 1
                # # serial_val_modify("page0.now_position.txt=\"缓冲液6液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^缓冲液6液位过低\")" % time1)  # 存入故障信息
                dox_command([24], err_inquiry_24)
                water_level_24 = [24]
            else:
                i2c43_t = 1
                # # serial_val_modify("page0.now_position.txt=\"缓冲液6液位过低\"")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^缓冲液6液位过低\")" % time1)  # 存入故障信息
                water_level_24 = [24]
        if i2c43 > YW43L and i2c43_t == 1 and i2c42 > YW42L:
            water_level_24 = [0]
            time.sleep(1)
            dox_command([0], err_inquiry_24)
            i2c43_t = 0
            # # serial_val_modify("page0.now_position.txt=\"缓冲液6液位正常\"")
    #     time.sleep(0.2)
    #     print("i2c40:" + str(i2c40))
    #     print("i2c41:" + str(i2c41))
    #     print("i2c42:" + str(i2c42))
    #     print("i2c43:" + str(i2c43))


def sql_insert_mode(num):  # 储存当前所处模式，在重启后可以回到重启前状态
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    mode_state = num
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT mode(mode,time) VALUE(%s,%s)", [(mode_state, time1)])

    cur = con.cursor()
    cur.execute("SELECT min(id) FROM mode ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM mode ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM mode " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def query_mode():  # 查询当前所处模式
    global mode
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select mode from mode where id =(SELECT max(id) FROM mode)")
    mode_now = int(cur.fetchone()[0])
    if mode_now == 1:
        #  #  # serial_val_modify("page0.now_status.txt=\"自动模式\"")
        serial_val_modify("homepage.mod_sw.val=1")
    elif mode_now == 0:
        #  #  # serial_val_modify("page0.now_status.txt=\"手动模式\"")
        serial_val_modify("homepage.mod_sw.val=0")
    mode = mode_now
    return mode_now


def restart_exe():  # 程序重启函数，用于自动转手动过程中重启，加速反馈
    python = sys.executable
    os.execl(python, python, *sys.argv)


def injection_port_0():
    injection_port(0)  # 切换阀复位,X-0指令

    print("切换阀初始化位置")


def recv():  # 接收串口信息
    while True:
        time.sleep(0.5)
        data = ser.read_all()
        if data == b'':
            continue
        else:
            break
    return data


def serial_read():  # 接收串口信息
    promptly = 0  # 用于快速判断串口信息，并做出反应
    while 1:
        global serial_txt, mode, serial_time
        data = recv()
        # 转换为十六进制字节流
        s_hex = binascii.hexlify(data)
        # 字节流转换成字符串
        s_hex = bytes.decode(s_hex)
        serial_txt = s_hex[0:6]
        serial_time = s_hex[0:28]
        print(serial_time)
        if promptly != serial_txt:  # 串口信息有变动就做出反应
            promptly = serial_txt
            if promptly == "ffff00":
                mode = 1  # 开启远程模式
                print("远程")
                sql_insert_mode(1)
            if promptly == "ffff01":
                mode = 0  # 开启就地模式
                print("就地")
                sql_insert_mode(0)


def serial_val_modify(val):  # 发送串口信息
    s = val
    # 字符串变gbk字符型字节流
    s_byte = s.encode("gbk")
    # 字符型字节流转十六进制字节流, 与b2a_hex()，bytes.hex()方法功能一样
    s_hex = binascii.hexlify(s_byte)
    s_hex = s_hex + b'FFFFFF'
    # 十六进制字节流解码
    s_gbk = codecs.decode(s_hex, 'hex')
    ser.write(s_gbk)


def can_open():  # 开启can通讯
    os.system('sudo ip link set can0 type can bitrate 250000')
    os.system('sudo ifconfig can0 up')


def io_restart():  # 重启can通讯
    global status_restart_timing
    # DHT11()
    status_restart_timing = 1
    while status_restart_timing == 1:  # 用于在io查询时其他程序不输送
        time.sleep(0.5)


def io_status():  # 重启can通讯
    while 1:
        # io_status_quit用于判断轮询是否退出，status_restart_timing用于判断是否重启can通讯
        global status_restart_timing, io_status_quit
        time.sleep(0.1)
        if status_restart_timing == 1 and io_status_quit == 1:
            status_restart_timing = 0
            print("查询dido状态重启")
            io()


def io():
    bus = can.Bus(interface='socketcan', channel='can0', receive_own_messages=eval('False'))
    while 1:
        global var1, var2, data_do, data_di  # 类型为list
        global status_restart_timing, io_status_quit  # 用于重启can通讯
        global do_location, di_location
        # 用于各种复位
        io_status_quit = 0
        if status_restart_timing == 1:
            print("查询dido状态关闭")
            os.system('sudo ifconfig can0 down')
            can_open()
            io_status_quit = 1
            break
        # global edge, l_s_time  # 长按和短按判断，类型为array元素为True/False
        inquiry_do = can.Message(arbitration_id=0x0201, data=[0x01], is_extended_id=False)  # 问询
        bus.send(inquiry_do)  # 0x0201 DO状态 返回513
        message_do = bus.recv(timeout=1)  # 接收数据
        type_id_do = message_do.arbitration_id  # 返回can报文类型
        if type_id_do == 513:
            # time_float1 = message_do.timestamp          # 浮点数时间戳
            # time_date1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(message_do.timestamp)))  # 转str时间
            # 取回4个字节,预留32通道,bytearray->int->bin(str)->去'0b'->bin->左补32位->反向排列
            data_do = '{:0>32b}'.format(int(bin(int.from_bytes(message_do.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            # print(data_do, type(data_do))
            if var1 != data_do:  # 差异检测，差异写入数据库
                # write_sql(time_date1, type_id_do, data_do)+++++++++++注意排列顺序有修改
                var1 = data_do  # 给中间值赋新值
                do_location = list(data_do)
        time.sleep(0.1)
        inquiry_di = can.Message(arbitration_id=0x0301, data=[0x01], is_extended_id=False)
        bus.send(inquiry_di)  # 0x0301  返回769/0x0301 di状态
        message_di = bus.recv(timeout=1)  # 接收数据
        type_id_di = message_di.arbitration_id  # 返回can报文类型
        if type_id_di == 769:
            # 取回4个字,预留32通道,bytearray->int->bin(str)->去'0b'->bin->左补32位->反向排列
            data_di = '{:0>32b}'.format(int(bin(int.from_bytes(message_di.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            # print(data_di, type(data_di))
            if data_di != var2:  # 差异检测，差异写入数据库
                # write_sql(time_date2, type_id_di, data_di)
                # for k in range(32):
                #     if var2[k] == '0' and data_di[k] == '1':  # 32点上升沿检测,没有检测到不复位
                #         edge[0, k] = 1  # 写入边缘检测edge[0]
                #         edge[1, k] = 0  # 复位下降沿
                #         # time_diff[0, k] = time.time()  # 把时间写入array[0]，上升沿触发时间戳
                #     if var2[k] == '1' and data_di[k] == '0':  # 32点下降沿检测,没有检测到不复位
                #         edge[1, k] = 1  # 写入边缘检测edge[1]
                #         edge[0, k] = 0  # 复位上升沿
                #         # time_diff[1, k] = time.time()  # 把时间写入array[1]，下降沿触发时间戳
                #     # l_s_time[0, k] = 0 < (time_diff[1, k] - time_diff[0, k]) < 3  # 短脉冲判断
                #     # l_s_time[1, k] = 3 < (time_diff[1, k] - time_diff[0, k]) < 10  # 长脉冲判断
                #     # print((time_diff[1, k] - time_diff[0, k]))
                var2 = data_di  # 给中间值赋新值
                di_location = list(data_di)  # 字符串转列表输出
        time.sleep(0.1)


def err_inquiry_function(err_num):  # 供dox_command函数使用，使某些do不复位
    if do_location[err_num - 1:err_num][0] == "1":
        err_inquiry_num = [err_num]
    else:
        err_inquiry_num = [0]
    return err_inquiry_num


def do_not_reset():
    while 1:
        time.sleep(0.2)
        global err_inquiry_s1, err_inquiry_s2, err_inquiry_s3, err_inquiry_s4_s10, err_inquiry_s01, err_inquiry_s14
        global err_inquiry_s14_21, err_inquiry_s15, err_inquiry_22, err_inquiry_23, err_inquiry_24, err_inquiry_21_24
        global err_inquiry_13, err_inquiry_14_16, err_inquiry_27_30, err_inquiry_s3_exit, err_inquiry_s10_s13
        global s4_state, s5_state, s6_state, s7_state, s8_state, err_inquiry_s3_exit_1_6, err_inquiry_s1_21
        global s9_state, s10_state, s11_state, s12_state, s13_state, s1_state, s2_state
        global DHT11_state_13, DHT11_state_14, DHT11_state_15, DHT11_state_16, err_inquiry_all, cut_signal_21
        # 用于系统复位
        err_inquiry_all = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            s2_state_21 + water_level_22 + \
            water_level_23 + water_level_24 + \
            err_inquiry_function(21) + \
            err_inquiry_function(22) + err_inquiry_function(23) + err_inquiry_function(24) + cut_signal_21 + error_20
        # 用于s1
        err_inquiry_s1 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + water_level_22 + \
            water_level_23 + water_level_24 + s5_state + \
            s4_state + s2_state + \
            s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + error_20
        # 用于s1
        err_inquiry_s1_21 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + water_level_22 + \
            water_level_23 + water_level_24 + s5_state + \
            s4_state + s2_state + \
            s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + s2_state_21 + error_20
        # 用于s1时的21
        err_inquiry_s2 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + water_level_22 + \
            water_level_23 + water_level_24 + s5_state + \
            s4_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + error_20
        # 用于s2
        err_inquiry_s3 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + water_level_22 + \
            water_level_23 + water_level_24 + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s10_state + error_20
        # 用于s3
        err_inquiry_s3_exit_1_6 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_23 + water_level_24 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state + error_20
        # 用于s3步骤1-6的复位
        err_inquiry_s3_exit = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_23 + water_level_24 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state + [21] + error_20 + cut_signal_21 + error_20
        # 用于s3,和手动状态下的首次复位
        err_inquiry_s4_s10 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_23 + water_level_24 + \
            s2_state + \
            s1_state + s13_state + s12_state + \
            s11_state + s10_state + error_20 + cut_signal_21 + err_inquiry_function(21) + error_20
        # s4_s10
        err_inquiry_s10_s13 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + err_inquiry_function(19) + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + err_inquiry_function(21) + \
            water_level_23 + water_level_24 + s5_state + \
            s4_state + s2_state + \
            s1_state + s9_state + \
            s8_state + \
            s7_state + s6_state + error_20 + cut_signal_21 + err_inquiry_function(1) + err_inquiry_function(2) + \
            err_inquiry_function(3) + err_inquiry_function(4) + err_inquiry_function(5) + \
            err_inquiry_function(6) + err_inquiry_function(7) + err_inquiry_function(8)
        # s10_s13
        err_inquiry_s14 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_23 + water_level_24 + cut_signal_21 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state + error_20
        # 用于自动状态下的首次复位,还有自动清洗
        err_inquiry_s14_21 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + err_inquiry_function(19) + \
            err_inquiry_function(20) + water_level_22 + \
            water_level_23 + water_level_24 + s5_state + \
            s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + error_20
        # s14的do21专用
        err_inquiry_s15 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + err_inquiry_function(19) + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_23 + water_level_24 + \
            s13_state + \
            s12_state + s11_state + \
            s10_state + [21] + error_20
        # s15专用
        err_inquiry_22 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + err_inquiry_function(19) + \
            err_inquiry_function(20) + s2_state_21 + \
            water_level_23 + water_level_24 + s5_state + \
            s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + err_inquiry_function(21) + error_20 + \
            err_inquiry_function(5) + err_inquiry_function(6) + \
            err_inquiry_function(7) + err_inquiry_function(8)
        # 清洗液低液位报警专用
        err_inquiry_23 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + err_inquiry_function(19) + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_24 + s5_state + \
            s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + err_inquiry_function(21) + error_20
        # 保护液低液位报警专用
        err_inquiry_24 = \
            DHT11_state_13 + DHT11_state_14 + \
            DHT11_state_15 + DHT11_state_16 + DHT11_state_17 + err_inquiry_function(19) + \
            err_inquiry_function(20) + s2_state_21 + water_level_22 + \
            water_level_23 + s5_state + \
            s4_state + s2_state + \
            s1_state + s13_state + s9_state + \
            s12_state + s8_state + s11_state + \
            s7_state + s10_state + s6_state + err_inquiry_function(21) + error_20
        err_inquiry_21_24 = \
            err_inquiry_function(21) + \
            err_inquiry_function(22) + err_inquiry_function(23) + err_inquiry_function(24)
        # 缓冲液低液位报警专用
        # err_inquiry_13 = \
        #     DHT11_state_13 + DHT11_state_14 + \
        #     DHT11_state_15 + DHT11_state_16 + err_inquiry_function(19) + \
        #     err_inquiry_function(20) + s2_state_21 + water_level_22 + \
        #     water_level_23 + water_level_24 + s5_state + \
        #     s4_state + s2_state + \
        #     s1_state + s13_state + s9_state + \
        #     s12_state + s8_state + s11_state + \
        #     s7_state + s10_state + s6_state + err_inquiry_function(21) + \
        #     err_inquiry_function(1) + err_inquiry_function(2) + err_inquiry_function(3) + \
        #     err_inquiry_function(4) + err_inquiry_function(5) + err_inquiry_function(6) + \
        #     err_inquiry_function(7) + err_inquiry_function(8) + err_inquiry_function(9) + \
        #     err_inquiry_function(10) + err_inquiry_function(11) + err_inquiry_function(12) + error_20
        # # 温湿度报警专用
        # err_inquiry_14_16 = \
        #     err_inquiry_function(19) + \
        #     err_inquiry_function(20) + s2_state_21 + water_level_22 + \
        #     water_level_23 + water_level_24 + s5_state + \
        #     s4_state + s2_state + \
        #     s1_state + s13_state + s9_state + \
        #     s12_state + s8_state + s11_state + \
        #     s7_state + s10_state + s6_state + err_inquiry_function(21) + \
        #     err_inquiry_function(1) + err_inquiry_function(2) + err_inquiry_function(3) + \
        #     err_inquiry_function(4) + err_inquiry_function(5) + err_inquiry_function(6) + \
        #     err_inquiry_function(7) + err_inquiry_function(8) + err_inquiry_function(9) + \
        #     err_inquiry_function(10) + err_inquiry_function(11) + err_inquiry_function(12) + error_20
        # 温湿度报警专用


def manual_control():  # 手动程序
    manual_control_io_restart_t = 0  # 用于手动状态下查询io状态的复位
    manual_control_reset = 0  # 刚切到手动状态，复位一次
    global serial_txt, recharge_t, err_inquiry_s3_exit_1_6, s2_state_21, err_inquiry_all, cut_signal_21, \
        maintenance_status, mode
    global s14_timing_judgement  # 自动清洗退出后，判断是否要在手动中计时
    s14_9_t7 = 0
    s14_9_t7_t = 1  # 为s14_9_t7计时做标定
    s14_24_C1_finally = 0
    time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
    serial_val_modify("warningpage.data0.insert(\"%s^手动模式\")" % time1)  # 待清除
    while 1:
        global do_location, di_location
        global manual_status, mode
        if mode == 1:
            manual_status = 0
            sql_DHT("jump_9", 1)  # 跳出手动的时候也跳出看门狗
            sql_DHT_del("manual_control")
            if s14_timing_judgement == 1:  # 如果是自动清洗中低液位自动跳出，则不断为S14中的第九步计时
                s14_9_t7_finally = s14_9_t7 + int(sql_query_s14_9_t7())
                if Txh != 0:
                    s14_24_C1_finally = int(sql_query_s14_24_C1()) + int(s14_9_t7 / Txh)
                elif Txh == 0:
                    s14_24_C1_finally = int(sql_query_s14_24_C1())
                sql_s14_time_storage("s14_9_t7", s14_9_t7_finally)
                sql_s14_time_storage("s14_24_C1", s14_24_C1_finally)
            else:  # 如果是自动清洗正常切到手动,则复位
                sql_s14_time_storage("s14_9_t7", 0)
                sql_s14_time_storage("s14_24_C1", 0)
            break

        if serial_txt == "ffff00":
            mode = 1  # 开启远程模式
            print("远程")
            sql_insert_mode(1)
            serial_txt = 0

        if manual_control_reset == 0:
            manual_control_reset = 1
            time.sleep(4)
            if di_location[4:5][0] == "0":
                s2_state_21 = [21]
            else:
                s2_state_21 = [0]
                cut_signal_21 = [0]
            time.sleep(2)
            dox_command([0], err_inquiry_s3_exit_1_6)  # 复位do
            # injection_port_0()  # 切换阀至0位

        if serial_txt == "050101":
            print("测量")
            serial_txt = 0
            manual_measurement_s1()  # 测量
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "050201":
            print("检修")
            serial_txt = 0
            manual_maintenance_s2()  # 检修
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "050301":
            sql_DHT("jump_9", 1)  # 跳出手动的时候也跳出看门狗
            sql_DHT_del("manual_control")
            manual_curing_s3()  # 手动保养
            # injection_port_0()  # 切换阀至0位
            maintenance_status = 0
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            # insert_error_num(0, 's3')

        if serial_txt == "070101":
            separate_water_flushing_s4()  # 单独水冲洗
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "070201":
            online_water_flushing_s5()  # 在线水冲洗
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "060101":
            clean_pump_s6()  # 输送清洗液
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "060301":
            protect_pump_s7()  # 输送保护液
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "060201":
            liquid_pump_4_s8()  # 输送缓冲液4
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        if serial_txt == "060401":
            liquid_pump_6_s9()  # 输送缓冲液6
            serial_txt = 0
            insert_error_num(0, 's15')
            insert_error_num(0, 's14')
            insert_error_num(0, 's3')

        time.sleep(0.1)
        manual_control_io_restart_t = manual_control_io_restart_t + 1
        s14_9_t7_t = s14_9_t7_t + 1
        if s14_9_t7_t >= 10:
            s14_9_t7_t = 0
            s14_9_t7 = s14_9_t7 + 1
        if manual_control_io_restart_t >= 300 and recharge_t == 0:
            manual_control_io_restart_t = 0
            sql_DHT("manual_control", s14_9_t7)  # 不断更新DHT表中的数据,喂看门狗
            sql_DHT_del("jump_9")
            print(i2c40, i2c41, i2c42, i2c43)
            io_restart()
            dox_command(err_inquiry_all, [0])


def manual_measurement_s1():
    global err_inquiry_s1, serial_txt, mode, s1_state, s2_state_21, err_inquiry_s1_21, error_20
    s1_t = 0  # 计时使用
    s1_state = [1]
    dox_command([1], err_inquiry_s1_21)  # 复位并输出DO1
    # # serial_val_modify("page0.now_position.txt=\"手动测量\"")
    serial_val_modify("homepage.now_status.txt=\"测量开启中\"")
    serial_val_modify("homepage.now_status.txt=\"测量开启中\"")
    time.sleep(0.1)
    while s1_t <= 20:  # 时限一分钟
        if serial_txt == "050100" or serial_txt == "050201":
            s1_state = [0]
            dox_command([0], err_inquiry_s1_21)  # 复位DO1
            serial_val_modify("homepage.now_status.txt=\"测量退出\"")
            serial_val_modify("homepage.now_status.txt=\"测量退出\"")
            return
        if di_location[4:5][0] == "1" or mode == 1:
            s1_state = [0]
            s2_state_21 = [0]
            dox_command([0], err_inquiry_s1)  # 复位DO1
            # # serial_val_modify("page5.measure.val=0")
            serial_val_modify("homepage.now_status.txt=\"测量位\"")
            serial_val_modify("homepage.now_status.txt=\"测量位\"")
            s1_t = 70  # 成功后跳出循环
            return
        else:
            s1_t = s1_t + 1  # 没成功时间计数+1
            time.sleep(1)
    if s1_t > 20 and di_location[4:5][0] == "0":
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^测量故障\")" % time1)  # 存入故障信息
        serial_val_modify("homepage.now_status.txt=\"测量故障\"")
        # # serial_val_modify("page0.now_position.txt=\"测量故障\"")
        s1_state = [0]
        dox_command([20], err_inquiry_s1_21)  # 输出do12
        error_20 = [20]
        # # serial_val_modify("page5.measure.val=0")
        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
        return


def manual_maintenance_s2():
    global err_inquiry_s2, serial_txt, mode, s2_state, s2_state_21, error_20
    s2_t = 0  # 计时使用
    s2_state = [2]
    dox_command([2], err_inquiry_s2)  # 复位并输出DO2
    print(do_location[1:2][0])
    # # serial_val_modify("page0.now_position.txt=\"手动检修\"")
    serial_val_modify("homepage.now_status.txt=\"检修开启中\"")
    serial_val_modify("homepage.now_status.txt=\"检修开启中\"")
    time.sleep(0.1)
    while s2_t <= 20:  # 时限一分钟
        if serial_txt == "050200" or serial_txt == "050101":
            s2_state = [0]
            dox_command([0], err_inquiry_s2)  # 复位DO2
            serial_val_modify("homepage.now_status.txt=\"检修退出\"")
            serial_val_modify("homepage.now_status.txt=\"检修退出\"")
            return
        if di_location[5:6][0] == "1" or mode == 1:
            s2_state = [0]
            s2_state_21 = [21]
            dox_command([21], err_inquiry_s2)  # 输出do21
            s2_t = 70  # 成功后跳出循环
            # serial_val_modify("page5.overhaul.val=0")
            serial_val_modify("homepage.now_status.txt=\"检修位\"")
            serial_val_modify("homepage.now_status.txt=\"检修位\"")
            return
        else:
            s2_t = s2_t + 1  # 没成功时间计数+1
            time.sleep(1)
    if s2_t > 20 and di_location[5:6][0] == "0":
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
        serial_val_modify("homepage.now_status.txt=\"检修故障\"")
        # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
        s2_state = [0]
        dox_command([20], err_inquiry_s2)  # 输出do3
        error_20 = [20]
        # serial_val_modify("page5.overhaul.val=0")
        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
        return


def manual_curing_s3():
    manual_curing_n = 1  # 用于记录保养的所处步骤
    serial_val_modify("homepage.now_status.txt=\"保养中\"")
    serial_val_modify("homepage.now_status.txt=\"保养中\"")
    # injection_port_0()
    global err_inquiry_s3, serial_txt, err_inquiry_s3_exit, err_inquiry_s3_exit_1_6
    global mode, atcal, flu, Tcx, Tqx1, Tqx2, Tbh, Tby, Thc, Txh
    global di_location, water_level_23, error_20, flu_t, maintenance_status
    global i2c40, i2c41, i2c42, i2c43
    special_jump_to_13 = 0
    manual_curing_error = 0
    try:
        manual_curing_error = sql_query_DHT("manual_curing")
    except Exception as e:
        # 处理所有异常错误
        print('Error:', e)
        pass
    if manual_curing_error > 0:  # 如果手动保养程序死机manual_curing_error会大于0，所以需要跳到步骤13
        insert_error_num(13, 's3')
    while 1:
        if do_location[21:22][0] == "1" or do_location[22:23][0] == "1":
            serial_val_modify("homepage.now_status.txt=\"保养结束-药剂不足\"")
            serial_val_modify("localpage.manual_warning.txt=\"手动保养取消-药剂不足\"")
            serial_val_modify("localpage.maintain_bt.val=0")
            return
        maintenance_status = 1
        step_val = error_val('s3')
        if step_val != 0 and step_val < 5:  # 用于切换阀故障时跳转步骤
            manual_curing_n = step_num('s3')
            if manual_curing_n == 13:
                manual_curing_n = 9
                special_jump_to_13 = 1
        elif step_val >= 5:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
            dox_command([20], err_inquiry_s3)  # 输出do3
            error_20 = [20]
            time.sleep(5)
            insert_error_num(0, 's3')
            return

        if di_location[5:6][0] == "1" and manual_curing_n != 12 and step_val == 0:
            manual_curing_n = 9

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit_1_6)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 1:
            print("手动保养步骤1")
            dox_command([21], err_inquiry_s3)
            #  # serial_val_modify("page0.now_status.txt=\"就地保养中\"")
            print("手动保养步骤2")
            serial_val_modify("homepage.now_status.txt=\"手动保养步骤2\"")
            if flu == 1:
                manual_curing_n = 3
            elif flu == 0:
                manual_curing_n = 4

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit_1_6)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 3:
            manual_curing_t3 = 0
            print("手动保养步骤3")
            serial_val_modify("homepage.now_status.txt=\"手动保养步骤3\"")
            dox_command([21, 4], err_inquiry_s3)
            print("手动保养步骤3时停")
            while manual_curing_t3 <= flu_t:
                manual_curing_t3 = manual_curing_t3 + 1
                time.sleep(1)
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit_1_6)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 4

        if manual_curing_n == 4:
            if flu == 1:
                dox_command([21, 4], err_inquiry_s3)
                print("手动保养步骤4")
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤4\"")
                injection_port(4)
                if insert_database() == "af06":  # 查询指令
                    manual_curing_n = 5
                    # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
                    insert_error_num(0, 's3')
                    pass  # 成功
                else:
                    if insert_database() == "af06":  # 查询指令
                        manual_curing_n = 5
                        # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
                        insert_error_num(0, 's3')
                        pass  # 成功
                    else:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀水冲洗位故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位故障\"")
                        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                        insert_error_num(4, 's3')
                        dox_command([20], err_inquiry_21_24)  # 输出do3
                        error_20 = [20]
                        return
            elif flu == 0:
                dox_command([21], err_inquiry_s3)
                print("手动保养步骤4")
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤4\"")
                injection_port(4)
                if insert_database() == "af06":  # 查询指令
                    manual_curing_n = 5
                    # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
                    insert_error_num(0, 's3')
                    pass  # 成功
                else:
                    if insert_database() == "af06":  # 查询指令
                        manual_curing_n = 5
                        # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
                        insert_error_num(0, 's3')
                        pass  # 成功
                    else:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀水冲洗位故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位故障\"")
                        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                        insert_error_num(4, 's3')
                        dox_command([20], err_inquiry_21_24)  # 输出do3
                        error_20 = [20]
                        return

        io_restart()  # 复位下dodi的读取
        time.sleep(1)

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit_1_6)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 5:
            if flu == 1:
                print("手动保养步骤5")
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤5\"")
                dox_command([21, 4, 3], err_inquiry_s3)  # 输出do25
                time.sleep(5)
                manual_curing_n = 6
            elif flu == 0:
                print("手动保养步骤5")
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤5\"")
                dox_command([21, 3], err_inquiry_s3)  # 输出do25
                time.sleep(5)
                manual_curing_n = 6

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit_1_6)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 6:
            if flu == 1:
                print("手动保养步骤6")
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤6\"")
                dox_command([21, 4, 3, 2], err_inquiry_s3)  # 输出do19
                manual_curing_n = 7
            elif flu == 0:
                print("手动保养步骤6")
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤6\"")
                dox_command([21, 3, 2], err_inquiry_s3)  # 输出do19
                manual_curing_n = 7

        if manual_curing_n == 7:
            print("手动保养步骤7")
            serial_val_modify("homepage.now_status.txt=\"手动保养步骤7\"")
            s2_t = 0  # 计时使用
            time.sleep(0.1)
            while s2_t <= 60:  # 时限一分钟
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
                if di_location[5:6][0] == "1":
                    dox_command([21, 3], err_inquiry_s3)  # 复位do2,4
                    serial_val_modify("homepage.state_sw.val=1")
                    s2_t = 70  # 成功后跳出循环
                    break
                else:
                    s2_t = s2_t + 1  # 没成功时间计数+1
                    time.sleep(1)
            if s2_t > 60 and di_location[5:6][0] == "0":
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                dox_command([20], err_inquiry_s3)  # 输出do3
                error_20 = [20]
                # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                return
            manual_curing_n = 8

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        io_restart()  # 复位下dido的问询
        time.sleep(1)

        if manual_curing_n == 8:
            print("手动保养步骤8")
            serial_val_modify("homepage.now_status.txt=\"手动保养步骤8\"")
            dox_command([21, 3], err_inquiry_s3)  # 复位do2,4
            manual_curing_t8 = 0
            while manual_curing_t8 <= Tcx:
                manual_curing_t8 = manual_curing_t8 + 1
                time.sleep(1)
                if mode == 1 or serial_txt == "050300":
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
            manual_curing_n = 9

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 9:
            print("手动保养步骤9")
            if special_jump_to_13 == 1:
                manual_curing_n = 10
            else:
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤9\"")
                dox_command([21], err_inquiry_s3)  # 复位do3
                injection_port(1)
                if insert_database() == "aa01":  # 查询指令
                    manual_curing_n = 10
                    # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
                    insert_error_num(0, 's3')
                    pass  # 成功
                else:
                    if insert_database() == "aa01":  # 查询指令
                        manual_curing_n = 10
                        # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
                        insert_error_num(0, 's3')
                        pass  # 成功
                    else:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-1位故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位故障\"")
                        # serial_val_modify("homepage.now_status.txt=\"输送故障\"")
                        insert_error_num(9, 's3')
                        dox_command([20], err_inquiry_21_24)  # 输出do3
                        error_20 = [20]
                        return

        if mode == 1 or serial_txt == "050300":
            dox_command([0], err_inquiry_s3_exit)  # 复位do
            # injection_port_0()  # 切换阀至0位
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            serial_val_modify("homepage.now_status.txt=\"保养结束\"")
            #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
            return

        if manual_curing_n == 10:
            print("手动保养步骤10")
            if special_jump_to_13 == 1:
                manual_curing_n = 11
            else:
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤10\"")
                dox_command([21, 5], err_inquiry_s3)  # 输出do20
                manual_curing_t10 = 0
                while manual_curing_t10 <= Tqx1:
                    manual_curing_t10 = manual_curing_t10 + 1
                    time.sleep(1)
                    if mode == 1 or serial_txt == "050300":
                        dox_command([0], err_inquiry_s3_exit)  # 复位do
                        # injection_port_0()  # 切换阀至0位
                        serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                        serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                        #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                        return
                manual_curing_n = 11

        io_restart()  # 复位下dido的问询
        time.sleep(1)

        if manual_curing_n == 11:
            print("手动保养步骤11")
            if special_jump_to_13 == 1:
                manual_curing_n = 12
            else:
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤11__电极浸泡中\"")
                dox_command([21], err_inquiry_s3)  # 复位do20
                manual_curing_t11 = 0
                s3_io_res_11 = 0
                while manual_curing_t11 <= Tqx2:
                    manual_curing_t11 = manual_curing_t11 + 1
                    time.sleep(1)
                    s3_io_res_11 = s3_io_res_11 + 1
                    if s3_io_res_11 >= 57:
                        s3_io_res_11 = 0
                        io_restart()  # 复位下dido的问询
                        time.sleep(3)
                        dox_command([21], err_inquiry_s3)
                    if mode == 1 or serial_txt == "050300":
                        dox_command([0], err_inquiry_s3_exit)  # 复位do
                        # injection_port_0()  # 切换阀至0位
                        serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                        serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                        #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                        return
                manual_curing_n = 12

        if manual_curing_n == 12:
            time.sleep(1)
            print("手动保养步骤12")
            if special_jump_to_13 == 1:
                manual_curing_n = 13
            else:
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤12\"")
                # while water_level_23 == [23]:
                #     time.sleep(1)
                #     if mode == 1 or serial_txt == "050300":
                #         dox_command([0], err_inquiry_s3_exit)  # 复位do
                #         # injection_port_0()  # 切换阀至0位
                #         serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                #         serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                #         #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                #         return

                dox_command([21, 6], err_inquiry_s3)  # 输出do21
                manual_curing_t12 = 0
                while manual_curing_t12 <= Tbh:
                    manual_curing_t12 = manual_curing_t12 + 1
                    time.sleep(1)
                    if mode == 1 or serial_txt == "050300":
                        dox_command([0], err_inquiry_s3_exit)  # 复位do
                        # injection_port_0()  # 切换阀至0位
                        serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                        serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                        #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                        return
                manual_curing_n = 13

        if manual_curing_n == 13:
            print("手动保养步骤13")
            if special_jump_to_13 == 1:
                manual_curing_n = 14
                insert_error_num(0, 's3')
            else:
                serial_val_modify("homepage.now_status.txt=\"手动保养步骤13\"")
                dox_command([21], err_inquiry_s3)  # 复位do6
                # injection_port_0()
                manual_curing_n = 14

        if manual_curing_n == 14:
            print("手动保养步骤14")
            special_jump_to_13 == 0
            serial_val_modify("homepage.now_status.txt=\"手动保养__电极养护中\"")
            T1 = 0
            if manual_curing_error > 0:
                T1 = manual_curing_error
            s3_io_res_14 = 0
            while T1 <= Tby:
                T1 = T1 + 1
                time.sleep(1)
                s3_io_res_14 = s3_io_res_14 + 1
                if s3_io_res_14 >= 57:
                    sql_DHT("manual_curing", T1)  # 不断更新DHT表中的数据,喂看门狗
                    sql_DHT_del("jump_9")  # 删除该跳数据是为了不跳出看门狗
                    s3_io_res_14 = 0
                    io_restart()  # 复位下dido的问询
                    time.sleep(3)
                    dox_command([21], err_inquiry_s3)
                    T1 = T1 + 6  # 手动补充时间
                if mode == 1 or serial_txt == "050300":
                    sql_DHT("jump_9", 1)  # 跳出手动的时候也跳出看门狗
                    sql_DHT_del("manual_curing")
                    dox_command([0], err_inquiry_s3_exit)  # 复位do
                    # injection_port_0()  # 切换阀至0位
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    serial_val_modify("homepage.now_status.txt=\"保养结束\"")
                    #  # serial_val_modify("page0.now_status.txt=\"就地保养结束\"")
                    return
                if do_location[22:23][0] == "1":
                    sql_DHT("jump_9", 1)  # 跳出手动的时候也跳出看门狗
                    sql_DHT_del("manual_curing")
                    serial_val_modify("homepage.now_status.txt=\"保养结束-药剂不足\"")
                    serial_val_modify("localpage.manual_warning.txt=\"手动保养取消-药剂不足\"")
                    serial_val_modify("localpage.maintain_bt.val=0")
                    insert_error_num(13, 's3')
                    return
            manual_curing_n = 12


def separate_water_flushing_s4():  # 电极水冲洗
    global err_inquiry_s4_s10, serial_txt, mode, s4_state, error_20
    # # serial_val_modify("page0.now_position.txt=\"单独水冲洗中\"")
    serial_val_modify("localpage.cavity_txt.txt=\"准备中\"")
    serial_val_modify("localpage.cavity_txt.txt=\"准备中\"")
    injection_port(4)
    s4_t1 = 1
    while serial_txt == "070100" or mode == 1:
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        return
    while s4_t1 <= 0:
        print("时停5秒")
        s4_t1 = s4_t1 + 1
        time.sleep(0.1)
        if serial_txt == "070100" or mode == 1:
            serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
            break
    if insert_database() == "af06":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
        pass  # 成功
    else:
        if insert_database() == "af06":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀水冲洗位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀水冲洗位故障\"")
            serial_val_modify("localpage.cavity_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.cavity_txt.txt=\"输送故障\"")
            dox_command([20], err_inquiry_21_24)  # 输出do3
            error_20 = [20]
            return
    while serial_txt == "070100" or mode == 1:
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.cavity_txt.txt=\"输送中\"")
    serial_val_modify("localpage.cavity_txt.txt=\"输送中\"")
    s4_state = [3]
    dox_command([3], err_inquiry_s4_s10)  # 输出do3
    while serial_txt != "070100":
        time.sleep(0.1)
        if mode == 1:
            break
    s4_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do3
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"单独水冲洗结束\"")
    serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.cavity_txt.txt=\"输送结束\"")


def online_water_flushing_s5():  # 支架水冲洗
    global err_inquiry_s4_s10, serial_txt, mode, s5_state
    # # serial_val_modify("page0.now_position.txt=\"在线水冲洗中\"")
    serial_val_modify("localpage.online_txt.txt=\"输送中\"")
    serial_val_modify("localpage.online_txt.txt=\"输送中\"")
    s5_state = [4]
    dox_command([4], err_inquiry_s4_s10)  # 输出do4
    while serial_txt != "070200":
        time.sleep(0.1)
        if mode == 1:
            break
    s5_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do4
    # # serial_val_modify("page0.now_position.txt=\"在线水冲洗结束\"")
    serial_val_modify("localpage.online_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.online_txt.txt=\"输送结束\"")


def clean_pump_s6():
    global err_inquiry_s4_s10, serial_txt, mode, s6_state, error_20
    # # serial_val_modify("page0.now_position.txt=\"输送清洗液中\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"准备中\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"准备中\"")
    injection_port(1)
    s6_t1 = 1
    while serial_txt == "060100" or mode == 1:
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        return
    while s6_t1 <= 0:
        print("时停5秒")
        s6_t1 = s6_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060100" or mode == 1:
            break
    if insert_database() == "aa01":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
        pass  # 成功
    else:
        if insert_database() == "aa01":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-1位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位故障\"")
            serial_val_modify("localpage.clean_pump_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.clean_pump_txt.txt=\"输送故障\"")
            dox_command([20], err_inquiry_21_24)  # 输出do3
            error_20 = [20]
            return
    while serial_txt == "060100" or mode == 1:
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送中\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送中\"")
    s6_state = [5]
    dox_command([5], err_inquiry_s4_s10)  # 输出do5
    while serial_txt != "060100":
        time.sleep(0.1)
        if mode == 1:
            break
    s6_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do20
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送清洗液结束\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.clean_pump_txt.txt=\"输送结束\"")


def protect_pump_s7():
    global err_inquiry_s4_s10, serial_txt, mode, s7_state, error_20
    # # serial_val_modify("page0.now_position.txt=\"输送保护液中\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"准备中\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"准备中\"")
    injection_port(1)
    s7_t1 = 1
    while serial_txt == "060300" or mode == 1:
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        return
    while s7_t1 <= 0:
        print("时停5秒")
        s7_t1 = s7_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060300" or mode == 1:
            break
    if insert_database() == "aa01":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
        pass  # 成功
    else:
        if insert_database() == "aa01":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-1位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-1位故障\"")
            serial_val_modify("localpage.pro_pump_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.pro_pump_txt.txt=\"输送故障\"")
            dox_command([20], err_inquiry_21_24)  # 输出do3
            error_20 = [20]
            return
    while serial_txt == "060300" or mode == 1:
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送中\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送中\"")
    s7_state = [6]
    dox_command([6], err_inquiry_s4_s10)  # 输出do6
    while serial_txt != "060300":
        time.sleep(0.1)
        if mode == 1:
            break
    s7_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do21
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送保护液结束\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.pro_pump_txt.txt=\"输送结束\"")


def liquid_pump_4_s8():
    global err_inquiry_s4_s10, serial_txt, mode, s8_state, error_20
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液4中\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"准备中\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"准备中\"")
    injection_port(3)
    s8_t1 = 1
    while serial_txt == "060200" or mode == 1:
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        return
    while s8_t1 <= 0:
        print("时停5秒")
        s8_t1 = s8_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060200" or mode == 1:
            break
    if insert_database() == "ac03":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-3位置\"")
        pass  # 成功
    else:
        if insert_database() == "ac03":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-3位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-3位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-3位故障\"")
            serial_val_modify("localpage.pump_4_txt.txt=\"输送故障\"")
            serial_val_modify("localpage.pump_4_txt.txt=\"输送故障\"")
            dox_command([20], err_inquiry_21_24)  # 输出do3
            error_20 = [20]
            return
    while serial_txt == "060200" or mode == 1:
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.pump_4_txt.txt=\"输送中\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"输送中\"")
    s8_state = [7]
    dox_command([7], err_inquiry_s4_s10)  # 输出do7
    while serial_txt != "060200":
        time.sleep(0.1)
        if mode == 1:
            break
    s8_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do7
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液4结束\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.pump_4_txt.txt=\"输送结束\"")


def liquid_pump_6_s9():
    global err_inquiry_s4_s10, serial_txt, mode, s9_state, error_20
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液6中\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"准备中\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"准备中\"")
    injection_port(2)
    while serial_txt == "060400" or mode == 1:
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        return
    s9_t1 = 1
    while s9_t1 <= 0:
        print("时停5秒")
        s9_t1 = s9_t1 + 1
        time.sleep(0.1)
        if serial_txt == "060400" or mode == 1:
            break
    if insert_database() == "ab02":  # 查询指令
        # # serial_val_modify("page0.now_position.txt=\"切换阀x-2位置\"")
        pass  # 成功
    else:
        if insert_database() == "ab02":  # 查询指令
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-2位置\"")
            pass  # 成功
        else:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀x-2位故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"切换阀x-2位故障\"")
            serial_val_modify("localpage.pump_6_txt.txt=\"输送故障\"")
            dox_command([20], err_inquiry_21_24)  # 输出do3
            error_20 = [20]
            return
    while serial_txt == "060400" or mode == 1:
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
        return
    serial_val_modify("localpage.pump_6_txt.txt=\"输送中\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"输送中\"")
    s9_state = [8]
    dox_command([8], err_inquiry_s4_s10)  # 输出do8
    while serial_txt != "060400":
        time.sleep(0.1)
        if mode == 1:
            break
    s9_state = [0]
    dox_command([0], err_inquiry_s4_s10)  # 复位do24
    # injection_port_0()
    # # serial_val_modify("page0.now_position.txt=\"输送缓冲液6结束\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")
    serial_val_modify("localpage.pump_6_txt.txt=\"输送结束\"")


def replenish_clean_supply_s10():
    global err_inquiry_s10_s13, serial_txt, mode, YW40H, i2c40, do_location, s10_state
    # # serial_val_modify("page0.now_position.txt=\"补充清洗液中\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充中\"")
    clean_supply_t = 0
    clean_supply_t2 = 0
    while i2c40 <= YW40H:
        if clean_supply_t == 0:
            clean_supply_t = 1
            s10_state = [9]
            dox_command([9], err_inquiry_s10_s13)  # 输出do9
        time.sleep(1)
        # water_content = (i2c40 / YW40H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c40 >= YW40H or serial_txt == "080100" or (GPIO.input(17) == 1 and GPIO.input(4) == 1):
            clean_supply_t2 = 1
            s10_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do9
            serial_txt = 0
            serial_val_modify("drugpage.clean_add.val=0")
            break
    if clean_supply_t2 == 0 or do_location[30:31][0] == "1":
        s10_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do27
        serial_txt = 0
        serial_val_modify("drugpage.clean_add.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充清洗液结束\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.clean_add_txt.txt=\"补充结束\"")


def replenish_protect_supply_s11():
    global err_inquiry_s10_s13, serial_txt, mode, YW41H, i2c41, do_location, s11_state
    # # serial_val_modify("page0.now_position.txt=\"补充保护液中\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充中\"")
    protect_supply_t = 0
    protect_supply_t2 = 0
    while i2c41 <= YW41H:
        if protect_supply_t == 0:
            protect_supply_t = 1
            s11_state = [10]
            dox_command([10], err_inquiry_s10_s13)  # 输出do10
        time.sleep(1)
        # water_content = (i2c41 / YW41H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c41 >= YW41H or serial_txt == "080300" or (GPIO.input(27) == 1 and GPIO.input(22) == 1):
            protect_supply_t2 = 1
            s11_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do28
            serial_txt = 0
            serial_val_modify("drugpage.protect_add.val=0")
            break
    if protect_supply_t2 == 0 or do_location[28:29][0] == "1":
        s11_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do28
        serial_txt = 0
        serial_val_modify("drugpage.protect_add.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充保护液结束\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.pro_add_txt.txt=\"补充结束\"")


def replenish_rehydration_4_s12():
    global err_inquiry_s10_s13, serial_txt, mode, YW42H, i2c42, do_location, s12_state
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液4中\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充中\"")
    rehydration_4_t = 0
    rehydration_4_t2 = 0
    while i2c42 <= YW42H:
        if rehydration_4_t == 0:
            rehydration_4_t = 1
            s12_state = [11]
            dox_command([11], err_inquiry_s10_s13)  # 输出do11
        time.sleep(1)
        # water_content = (i2c42 / YW42H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c42 >= YW42H or serial_txt == "080200" or (GPIO.input(6) == 1 and GPIO.input(5) == 1):
            rehydration_4_t2 = 1
            s12_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do29
            serial_txt = 0
            serial_val_modify("drugpage.add_4.val=0")
            break
    if rehydration_4_t2 == 0 or do_location[26:27][0] == "1":
        s12_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do29
        serial_txt = 0
        serial_val_modify("drugpage.add_4.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液4结束\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.add_4_txt.txt=\"补充结束\"")


def replenish_rehydration_6_s13():
    global err_inquiry_s10_s13, serial_txt, mode, YW43H, i2c43, do_location, err_inquiry_27_30, s13_state
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液6中\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充中\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充中\"")
    rehydration_6_t = 0
    rehydration_6_t2 = 0
    while i2c43 <= YW43H:
        if rehydration_6_t == 0:
            rehydration_6_t = 1
            s13_state = [12]
            dox_command([12], err_inquiry_s10_s13)  # 输出do12
        time.sleep(1)
        # water_content = (i2c43 / YW43H) * 100
        # serial_val_modify("drugpage.water_content.val=%s" % water_content)
        if i2c43 >= YW43H or serial_txt == "080400" or (GPIO.input(13) == 1 and GPIO.input(26) == 1):
            rehydration_6_t2 = 1
            s13_state = [0]
            dox_command([0], err_inquiry_s10_s13)  # 复位do30
            serial_txt = 0
            serial_val_modify("drugpage.add_6.val=0")
            break
    if rehydration_6_t2 == 0 or do_location[24:25][0] == "1":
        s13_state = [0]
        dox_command([0], err_inquiry_s10_s13)  # 复位do30
        serial_txt = 0
        serial_val_modify("drugpage.add_6.val=0")
    # # serial_val_modify("page0.now_position.txt=\"补充缓冲液6结束\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充结束\"")
    serial_val_modify("drugpage.add_6_txt.txt=\"补充结束\"")


def automatic_cleaning_s14():
    global err_inquiry_s14_21, serial_txt, mode, di_location, automatic_cleaning_status
    global flu, atcal, err_inquiry_s14, do_location, recharge_t
    global Tqx1, Tqx2, Tby, Txh, Tco, Thc, err_inquiry_s14, error_20, flu_t, atcal_t, cut_signal_21
    global is_require_s14
    global i2c40, i2c41, i2c42, i2c43
    automatic_cleaning_n = 0
    C1 = 0
    C1_time = 0
    FST = 0
    special_jump_to_17 = 0
    special_jump_to_20 = 0
    special_jump_to_25 = 0
    special_jump_to_30 = 0
    s14_switching = 0  # 用于判断校准后运行为0，还是运行后校准为1
    try:
        s14_switching = sql_query_s14_switching()
    except Exception as e:
        # 处理所有异常错误
        print('Error:', e)
        pass

    s14_error = 0
    try:
        s14_error = sql_query_DHT("s14_t7")
    except Exception as e:
        # 处理所有异常错误
        # print('Error:', e)
        pass
    if s14_error > 0:  # 如果手动保养程序死机s15_error会大于0，所以需要跳到步骤13
        insert_error_num(7, 's14')

    while 1:
        if automatic_cleaning_n == 0:
            s14_t0 = 0  # 计时使用
            while s14_t0 <= 50:  # 时限5秒
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    return
                if do_location[21:22][0] == "1":
                    automatic_cleaning_status = 0
                    return
                s14_t0 = s14_t0 + 1  # 没成功时间计数+1
                time.sleep(0.1)
            print("\n" + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
            print("自动清洗开启中")
            automatic_cleaning_n = 1

        step_val = error_val('s14')
        if step_val != 0 and step_val < 5:  # 用于切换阀故障时跳转步骤
            automatic_cleaning_n = step_num('s14')
            if automatic_cleaning_n == 17:  # 距离17过远，做个简易跳转
                automatic_cleaning_n = 12
                special_jump_to_17 = 1
            elif automatic_cleaning_n == 20:  # 距离20过远，做个简易跳转
                automatic_cleaning_n = 12
                special_jump_to_17 = 1
                special_jump_to_20 = 1
            elif automatic_cleaning_n == 25:  # 距离25过远，做个简易跳转
                automatic_cleaning_n = 12
                special_jump_to_17 = 1
                special_jump_to_20 = 1
                special_jump_to_25 = 1
            elif automatic_cleaning_n == 30:  # 距离30过远，做个简易跳转
                automatic_cleaning_n = 12
                special_jump_to_17 = 1
                special_jump_to_20 = 1
                special_jump_to_25 = 1
                special_jump_to_30 = 1
        elif step_val >= 5:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
            dox_command([20], err_inquiry_s3)  # 输出do3
            error_20 = [20]
            time.sleep(5)
            insert_error_num(0, 's14')
            return

        if automatic_cleaning_n == 1:
            print("自动清洗步骤1")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤1\"")
            serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
            serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^自动模式\")" % time1)  # 待清除
            dox_command([21, 19], err_inquiry_s14)  # 输出do19
            time.sleep(3)
            cut_signal(1)
            if atcal == 0 or s14_switching == 1:  # 如果没有校准功能则跳到步骤2，s14_switching运行后校准为1，校准后运行为0
                automatic_cleaning_n = 2
                FST = 0
            elif atcal == 1 and do_location[23:24][0] == "0" and s14_switching == 0:
                FST = 1
                automatic_cleaning_n = 10
            elif atcal == 1 and do_location[23:24][0] == "1":
                automatic_cleaning_status = 0
                return

        if mode == 0 or di_location[0:1][0] == "1" or automatic_cleaning_n == 999:
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 2:
            step_val = error_val('s14')
            if step_val == 0:
                special_jump_to_17 = 0
                special_jump_to_20 = 0
                special_jump_to_25 = 0
                special_jump_to_30 = 0
            dox_command([21, 19], err_inquiry_s14)  # 输出do19
            time.sleep(1)
            cut_signal_21 = [21]
            print("自动清洗步骤2")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤2\"")
            injection_port(4)  # 切换阀复位,X-4指令
            time.sleep(5)
            if insert_database() == "af06":  # 查询指令
                # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                insert_error_num(0, 's14')
                pass  # 成功
            else:
                time.sleep(3)
                if insert_database() == "af06":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    insert_error_num(0, 's14')
                    pass  # 成功
                else:
                    # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                    # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                    insert_error_num(2, 's14')
                    automatic_cleaning_status = 0
                    return
            automatic_cleaning_n = 3

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 3:
            print("自动清洗步骤3")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤3\"")
            dox_command([19, 3], err_inquiry_s14)  # 输出DO22
            time.sleep(5)
            automatic_cleaning_n = 4

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            dox_command([19], err_inquiry_s14)
            return

        if automatic_cleaning_n == 4:
            print("自动清洗步骤4")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤4\"")
            dox_command([19, 3, 1], err_inquiry_s14)  # 输出DO24
            s14_t4 = 0  # 计时使用
            while s14_t4 <= 60:  # 时限一分钟
                if di_location[4:5][0] == "1" or mode == 0:
                    dox_command([19], err_inquiry_s14)  # 复位DO24
                    s14_t4 = 70  # 成功后跳出循环
                    automatic_cleaning_n = 6
                    serial_val_modify("homepage.state_sw.val=0")
                    break
                else:
                    s14_t4 = s14_t4 + 1  # 没成功时间计数+1
                    time.sleep(1)
            if s14_t4 > 60 and di_location[4:5][0] == "0":
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^测量故障\")" % time1)  # 存入故障信息
                # # serial_val_modify("page0.now_position.txt=\"测量故障\"")
                dox_command([20], err_inquiry_s14)  # 输出do3
                error_20 = [20]
                # # serial_val_modify("page5.measure.val=0")
                return

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            dox_command([19], err_inquiry_s14)
            return

        if automatic_cleaning_n == 6:
            print("自动清洗步骤6")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤6__等待数值稳定\"")
            dox_command([19], err_inquiry_s14)  # do24,22
            # injection_port_0()
            s14_t6 = 0
            s14_io_res_6 = 0
            print("时停40秒")
            while s14_t6 <= 40:
                s14_t6 = s14_t6 + 1
                time.sleep(1)
                s14_io_res_6 = s14_io_res_6 + 1
                if s14_io_res_6 >= 30 and recharge_t == 0:
                    s14_io_res_6 = 0
                    io_restart()  # 复位下dido的问询
                    time.sleep(1)
                    dox_command([19], err_inquiry_s14)  # do24,22
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    return
            automatic_cleaning_n = 7

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        s14_t7 = 0
        if s14_error > 0:
            s14_t7 = s14_error
        if automatic_cleaning_n == 7:
            print("自动清洗步骤7")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤7\"")
            dox_command([19], err_inquiry_s14)  # do24,22
            time.sleep(3)
            cut_signal(0)  # 复位DO13
            print("切除信号关闭")
            if do_location[21:22][0] == "0":
                insert_error_num(0, 's14')
                automatic_cleaning_n = 9
            elif do_location[21:22][0] == "1" or (atcal == 1 and do_location[23:24][0] == "1"):  # 药剂不足退出循环设置手动
                automatic_cleaning_n = 0
                insert_error_num(7, 's14')
                sql_s14_time_storage("s14_9_t7", 0)
                return
            '''
            else:
                while 1:
                    s14_t7 = s14_t7 + 1
                    time.sleep(1)
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    if do_location[21:22][0] == "0":
                        automatic_cleaning_n = 9
                        break
            '''

        if automatic_cleaning_n == 9:
            print("自动清洗步骤9")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤9__正常测量中\"")
            # serial_val_modify("homepage.now_status.txt=\"正常测量中\"")
            s14_io_res_9 = 0
            print("TXH", int(Txh))
            sql_DHT_del("jump_9")
            if is_require_s14 == 1:
                s14_t7 = int(sql_query_s14_9_t7())
                print("自动清洗延时时间", s14_t7)
            while 1:
                s14_t7 = s14_t7 + 1
                time.sleep(1)
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    sql_DHT("jump_9", s14_t7)  # 用于跳出看门狗的循环
                    sql_DHT_del("s14_t7")
                    return
                s14_io_res_9 = s14_io_res_9 + 1
                if do_location[21:22][0] == "1" or (atcal == 1 and do_location[23:24][0] == "1"):  # 药剂不足退出循环设置手动
                    automatic_cleaning_n = 0
                    insert_error_num(7, 's14')
                    sql_s14_time_storage("s14_9_t7", s14_t7)  # 药剂不足退出状态下储存S14第九步的时间
                    sql_DHT("jump_9", s14_t7)
                    sql_DHT_del("s14_t7")
                    return
                if s14_io_res_9 >= 57 and recharge_t == 0:
                    s14_io_res_9 = 0
                    io_restart()  # 复位下dido的问询
                    cut_signal(0)
                    cut_signal_21 = [0]
                    time.sleep(3)
                    dox_command([19], err_inquiry_s14)
                    s14_t7 = s14_t7 + 6  # 手动补充时间
                    sql_DHT("s14_t7", s14_t7)
                if s14_t7 >= int(Txh):  # 临时修改T1时间，待修复
                    automatic_cleaning_n = 10
                    sql_s14_time_storage("s14_9_t7", 0)
                    sql_DHT("jump_9", s14_t7)
                    sql_DHT_del("s14_t7")
                    break
                # print("自动清洗延时时间", s14_t7)

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            return

        if automatic_cleaning_n == 10:
            injection_port_0()
            print("自动清洗步骤10")
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^自动清洗开始\")" % time1)  # 待清除
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤10\"")
            # serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
            cut_signal_21 = [21]
            cut_signal(1)
            time.sleep(3)
            dox_command([21, 19], err_inquiry_s14)  # 输出DO19
            print("切除信号开启")
            if flu == 0:
                automatic_cleaning_n = 12
            elif flu == 1:
                automatic_cleaning_n = 11

        if automatic_cleaning_n == 11:
            print("自动清洗步骤11")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤11\"")
            dox_command([19, 4], err_inquiry_s14)  # 输出DO20
            s14_t11 = 0
            while 1:
                if mode == 0 or di_location[0:1][0] == "1":
                    automatic_cleaning_status = 0
                    dox_command([19], err_inquiry_s14)
                    return
                s14_t11 = s14_t11 + 1
                time.sleep(1)
                if s14_t11 >= flu_t:
                    automatic_cleaning_n = 12
                    break
            print(do_location)

        if mode == 0 or di_location[0:1][0] == "1":
            automatic_cleaning_status = 0
            dox_command([19], err_inquiry_s14)
            return

        if automatic_cleaning_n == 12:
            print("自动清洗步骤12")
            serial_val_modify("homepage.now_status.txt=\"自动清洗步骤12\"")
            if special_jump_to_17 == 1:
                automatic_cleaning_n = 13
            else:
                if flu == 0:  # 0为不输出do26
                    dox_command([21, 19], err_inquiry_s14)  # 输出DO20
                    cut_signal_21 = [21]
                    injection_port(4)  # 切换阀复位,X-4指令
                    time.sleep(5)
                    if insert_database() == "af06":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                        automatic_cleaning_n = 13
                        insert_error_num(0, 's14')
                        pass  # 成功
                    else:
                        time.sleep(3)
                        if insert_database() == "af06":  # 查询指令
                            # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                            automatic_cleaning_n = 13
                            insert_error_num(0, 's14')
                            pass  # 成功
                        else:
                            # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                            # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                            insert_error_num(12, 's14')
                            automatic_cleaning_status = 0
                            return
                elif flu == 1:
                    dox_command([21, 19, 4], err_inquiry_s14)  # 输出DO20
                    cut_signal_21 = [21]
                    injection_port(4)  # 切换阀复位,X-4指令
                    time.sleep(5)
                    if insert_database() == "af06":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                        automatic_cleaning_n = 13
                        insert_error_num(0, 's14')
                        pass  # 成功
                    else:
                        time.sleep(3)
                        if insert_database() == "af06":  # 查询指令
                            # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                            automatic_cleaning_n = 13
                            insert_error_num(0, 's14')
                            pass  # 成功
                        else:
                            # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                            # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                            insert_error_num(12, 's14')
                            automatic_cleaning_status = 0
                            dox_command([21, 19], err_inquiry_s14)  # 出现故障关闭do4
                            return

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                dox_command([21, 19], err_inquiry_s14)
                return
            if recharge_t == 0:
                io_restart()
                time.sleep(1)

            if automatic_cleaning_n == 13:
                print("自动清洗步骤13")
                serial_val_modify("homepage.now_status.txt=\"自动清洗步骤13\"")
                if special_jump_to_17 == 1:
                    automatic_cleaning_n = 14
                else:
                    if flu == 0:  # 0为不输出do26
                        dox_command([19, 3], err_inquiry_s14)  # 输出DO22
                        time.sleep(5)
                        automatic_cleaning_n = 14
                    elif flu == 1:
                        dox_command([19, 4, 3], err_inquiry_s14)  # 输出DO22
                        time.sleep(5)
                        automatic_cleaning_n = 14

            if automatic_cleaning_n == 14:
                print("自动清洗步骤14")
                serial_val_modify("homepage.now_status.txt=\"自动清洗步骤14\"")
                if special_jump_to_17 == 1:
                    automatic_cleaning_n = 16
                else:
                    if flu == 0:  # 0为不输出do26
                        dox_command([19, 3, 2], err_inquiry_s14)  # 输出DO23
                        s14_14 = 0
                        while s14_14 <= 60:  # 时限一分钟
                            if di_location[5:6][0] == "1" or mode == 0:
                                s14_14 = 70  # 成功后跳出循环
                                automatic_cleaning_n = 16
                                serial_val_modify("homepage.state_sw.val=1")
                                break
                            else:
                                s14_14 = s14_14 + 1  # 没成功时间计数+1
                                time.sleep(1)
                        if s14_14 > 60 and di_location[5:6][0] == "0":
                            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                            # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                            dox_command([20], err_inquiry_s3)  # 输出do3
                            error_20 = [20]
                            return
                    elif flu == 1:
                        dox_command([19, 4, 3, 2], err_inquiry_s14)  # 输出DO23
                        s14_14 = 0
                        while s14_14 <= 60:  # 时限一分钟
                            if di_location[5:6][0] == "1" or mode == 0:
                                s14_14 = 70  # 成功后跳出循环
                                automatic_cleaning_n = 16
                                serial_val_modify("homepage.state_sw.val=1")
                                break
                            else:
                                s14_14 = s14_14 + 1  # 没成功时间计数+1
                                time.sleep(1)
                        if s14_14 > 60 and di_location[5:6][0] == "0":
                            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                            # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                            dox_command([20], err_inquiry_s3)  # 输出do3
                            error_20 = [20]
                            return

            if automatic_cleaning_n == 16:
                print("自动清洗步骤16")
                serial_val_modify("homepage.now_status.txt=\"自动清洗步骤16\"")
                if special_jump_to_17 == 1:
                    automatic_cleaning_n = 17
                else:
                    dox_command([19, 3], err_inquiry_s14)  # 复位do23,do20
                    s14_t15 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            dox_command([19], err_inquiry_s14)
                            return
                        s14_t15 = s14_t15 + 1
                        time.sleep(1)
                        if s14_t15 >= Tcx:
                            automatic_cleaning_n = 17
                            break

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                dox_command([19], err_inquiry_s14)
                return
            if recharge_t == 0:
                io_restart()
                time.sleep(1)

            if automatic_cleaning_n == 17:
                print("自动清洗步骤17")
                serial_val_modify("homepage.now_status.txt=\"自动清洗步骤17\"")
                if special_jump_to_20 == 1:
                    automatic_cleaning_n = 18
                else:
                    cut_signal_21 = [21]
                    time.sleep(2)
                    dox_command([19], err_inquiry_s14)  # 复位do22
                    injection_port(1)  # 切换阀复位,X-1指令
                    time.sleep(5)
                    if insert_database() == "aa01":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                        automatic_cleaning_n = 18
                        insert_error_num(0, 's14')
                        pass  # 成功
                    else:
                        time.sleep(3)
                        if insert_database() == "aa01":  # 查询指令
                            # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                            automatic_cleaning_n = 18
                            insert_error_num(0, 's14')
                            pass  # 成功
                        else:
                            # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                            # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                            insert_error_num(17, 's14')
                            automatic_cleaning_status = 0
                            return

            if automatic_cleaning_n == 18:
                print("自动清洗步骤18")
                serial_val_modify("homepage.now_status.txt=\"自动清洗步骤18\"")
                if special_jump_to_20 == 1:
                    automatic_cleaning_n = 19
                else:
                    dox_command([19, 5], err_inquiry_s14)  # 输出do32
                    s14_t18 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            dox_command([19], err_inquiry_s14)
                            return
                        s14_t18 = s14_t18 + 1
                        time.sleep(1)
                        if s14_t18 >= Tqx1:
                            automatic_cleaning_n = 19
                            break

            if automatic_cleaning_n == 19:
                print("自动清洗步骤19")
                serial_val_modify("homepage.now_status.txt=\"自动清洗步骤19__电极浸泡中\"")
                if special_jump_to_20 == 1:
                    automatic_cleaning_n = 20
                else:
                    dox_command([19], err_inquiry_s14)  # 复位do32
                    s14_t19 = 0
                    s14_io_res_19 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            return
                        s14_t19 = s14_t19 + 1
                        time.sleep(1)
                        s14_io_res_19 = s14_io_res_19 + 1
                        if s14_io_res_19 >= 60 and recharge_t == 0:
                            s14_io_res_19 = 0
                            io_restart()  # 复位下dido的问询
                            time.sleep(1)
                            dox_command([19], err_inquiry_s14)  # 复位do32
                        if s14_t19 >= Tqx2:
                            automatic_cleaning_n = 20
                            break

            if automatic_cleaning_n == 20:
                if special_jump_to_25 == 1:
                    automatic_cleaning_n = 21
                else:
                    cut_signal_21 = [21]
                    dox_command([21, 19], err_inquiry_s14)
                    print("自动清洗步骤20")
                    serial_val_modify("homepage.now_status.txt=\"自动清洗步骤20\"")
                    injection_port(4)  # 切换阀复位,X-4指令
                    time.sleep(5)
                    if insert_database() == "af06":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                        automatic_cleaning_n = 21
                        insert_error_num(0, 's14')
                        pass  # 成功
                    else:
                        time.sleep(3)
                        if insert_database() == "af06":  # 查询指令
                            # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                            automatic_cleaning_n = 21
                            insert_error_num(0, 's14')
                            pass  # 成功
                        else:
                            # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                            # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                            insert_error_num(20, 's14')
                            automatic_cleaning_status = 0
                            dox_command([20], err_inquiry_s3)
                            return

            if recharge_t == 0:
                io_restart()
                time.sleep(1)

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                dox_command([19, 21], err_inquiry_s14)
                return

            if automatic_cleaning_n == 21:
                if special_jump_to_25 == 1:
                    automatic_cleaning_n = 22
                else:
                    print("自动清洗步骤21")  # 输出 DO3，延时 0.5*Tcx 后，复位 DO3,延时2分钟，输出 DO3，延时 0.5*Tcx，进入下一步
                    serial_val_modify("homepage.now_status.txt=\"自动清洗步骤21\"")
                    dox_command([19, 3], err_inquiry_s14)  # 输出do22
                    s14_t21 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            dox_command([19], err_inquiry_s14)
                            return
                        s14_t21 = s14_t21 + 1
                        time.sleep(1)
                        if s14_t21 >= Tcx * 0.5:
                            # automatic_cleaning_n = 22
                            break
                    dox_command([19], err_inquiry_s14)  # 输出do22
                    s14_t21 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            dox_command([19], err_inquiry_s14)
                            return
                        s14_t21 = s14_t21 + 1
                        time.sleep(1)
                        if s14_t21 >= 120:
                            # automatic_cleaning_n = 22
                            break
                    s14_t21 = 0
                    dox_command([19, 3], err_inquiry_s14)  # 输出do22
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            dox_command([19], err_inquiry_s14)
                            return
                        s14_t21 = s14_t21 + 1
                        time.sleep(1)
                        if s14_t21 >= Tcx * 0.5:
                            automatic_cleaning_n = 22
                            break

            if automatic_cleaning_n == 22:
                if special_jump_to_25 == 1:
                    automatic_cleaning_n = 23
                else:
                    print("自动清洗步骤22")
                    serial_val_modify("homepage.now_status.txt=\"自动清洗步骤22\"")
                    dox_command([19], err_inquiry_s14)  # 复位do22
                    automatic_cleaning_n = 23

            if automatic_cleaning_n == 23:
                if special_jump_to_25 == 1:
                    automatic_cleaning_n = 24
                else:
                    print("自动清洗步骤23")
                    serial_val_modify("homepage.now_status.txt=\"自动清洗步骤23\"")
                    if atcal == 1:
                        automatic_cleaning_n = 24
                        C1 = int(sql_query_s14_24_C1()) + 1
                        sql_s14_time_storage("s14_24_C1", C1)
                        C1_time = C1 * Txh + 2940 * C1  # 补齐校准步骤时间
                    elif atcal == 0:
                        automatic_cleaning_n = 36

            if automatic_cleaning_n == 24:
                if special_jump_to_25 == 1:
                    automatic_cleaning_n = 25
                else:
                    print("自动清洗步骤24")
                    print("C1_time:", C1_time)
                    print("Tco:", Tco)
                    print("FST：", FST)
                    print("do_location[23:24][0]：", do_location[23:24][0])
                    time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                    serial_val_modify("warningpage.data0.insert(\"%s^自动清洗完成\")" % time1)  # 待清除
                    serial_val_modify("homepage.now_status.txt=\"自动校准24\"")
                    if is_require_s14 == 1:
                        C1_time = int(sql_query_s14_24_C1()) * Txh + 2940 * C1  # 补齐校准步骤时间
                    if (C1_time >= Tco and do_location[23:24][0] == "0") or (FST == 1):
                        print("进入自动校准")
                        # serial_val_modify("homepage.now_status.txt=\"自动校准中\"")
                        # serial_val_modify("homepage.now_status.txt=\"自动校准中\"")
                        # PH_choice(4)  # 选择校准模式
                        time.sleep(3)
                        automatic_cleaning_n = 25
                    elif C1_time >= Tco and do_location[23:24][0] == "1":
                        automatic_cleaning_status = 0
                        insert_error_num(7, 's14')  # 药剂不足跳出
                        return
                    else:
                        automatic_cleaning_n = 36
                        print("进入步骤36")

            if automatic_cleaning_n == 25:
                if special_jump_to_30 == 1:
                    automatic_cleaning_n = 26
                else:
                    print("自动清洗步骤25")  # 切换阀动作
                    time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                    serial_val_modify("warningpage.data0.insert(\"%s^自动校准开始\")" % time1)  # 存入故障信息
                    serial_val_modify("homepage.now_status.txt=\"自动校准6.86__25\"")
                    FST = 0
                    s14_t25 = 0
                    cut_signal_21 = [21]
                    dox_command([21, 19], err_inquiry_s14)
                    injection_port(2)  # 切换阀复位,X-4指令
                    time.sleep(5)
                    if insert_database() == "ab02":  # 查询指令
                        insert_error_num(0, 's14')
                        pass  # 成功
                    else:
                        time.sleep(3)
                        if insert_database() == "ab02":  # 查询指令
                            insert_error_num(0, 's14')
                            pass  # 成功
                        else:
                            # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                            insert_error_num(25, 's14')
                            automatic_cleaning_status = 0
                            return
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            return
                        s14_t25 = s14_t25 + 1
                        time.sleep(1)
                        if s14_t25 >= 4:
                            automatic_cleaning_n = 26
                            break
                        if atcal == 0:
                            automatic_cleaning_n = 36
                            break

            if automatic_cleaning_n == 26:
                if special_jump_to_30 == 1:
                    automatic_cleaning_n = 27
                else:
                    print("自动清洗步骤26")  # 打校准液
                    serial_val_modify("homepage.now_status.txt=\"自动校准6.86__26\"")
                    dox_command([19, 8], err_inquiry_s14)  # 输出do26
                    s14_t26 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            dox_command([19], err_inquiry_s14)
                            return
                        s14_t26 = s14_t26 + 1
                        time.sleep(1)
                        if s14_t26 >= Thc / 2:
                            automatic_cleaning_n = 27
                            break
                        if atcal == 0:
                            automatic_cleaning_n = 36
                            break

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                dox_command([19], err_inquiry_s14)
                return

            if automatic_cleaning_n == 27:
                if special_jump_to_30 == 1:
                    automatic_cleaning_n = 28
                else:
                    print("自动清洗步骤27")  # 再打校准液,并且等待校准液稳定
                    serial_val_modify("homepage.now_status.txt=\"自动校准6.86__27\"")
                    dox_command([19], err_inquiry_s14)  # 复位do26
                    s14_t27_1 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            return
                        s14_t27_1 = s14_t27_1 + 1
                        time.sleep(1)
                        if s14_t27_1 >= 5:
                            break
                        if atcal == 0:
                            automatic_cleaning_n = 36
                            break
                    dox_command([19, 8], err_inquiry_s14)  # 输出do8
                    s14_t260 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            return
                        s14_t260 = s14_t260 + 1
                        time.sleep(1)
                        if s14_t260 >= Thc / 2:
                            break
                        if atcal == 0:
                            automatic_cleaning_n = 36
                            break
                    dox_command([19], err_inquiry_s14)  # 复位do8
                    s14_t27 = 0
                    while 1:
                        if mode == 0 or di_location[0:1][0] == "1":
                            automatic_cleaning_status = 0
                            return
                        s14_t27 = s14_t27 + 1
                        time.sleep(1)
                        if s14_t27 >= 2:
                            automatic_cleaning_n = 28
                            break
                        if atcal == 0:
                            automatic_cleaning_n = 36
                            break

            if automatic_cleaning_n == 28:
                if special_jump_to_30 == 1:
                    automatic_cleaning_n = 29
                else:
                    time.sleep(3)
                    print("自动清洗步骤28")
                    serial_val_modify("homepage.now_status.txt=\"自动校准6.86__28\"")
                    adjustCounter = 0
                    adjustFlag = 0
                    while adjustCounter < 3 and adjustFlag == 0:
                        status_now_6 = PH_enter_calibration(6)
                        print("28_enter_: " + status_now_6)
                        if status_now_6 == "0186030261":
                            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                            serial_val_modify("warningpage.data0.insert(\"%s^校准6.86启动失败\")" % time1)
                            dox_command([20], err_inquiry_s14)  # 输出故障20
                            error_20 = [20]
                            automatic_cleaning_n = 37
                            adjustFlag = 1
                        elif status_now_6 == "010600010000d80a":
                            automatic_cleaning_n = 29
                            adjustFlag = 1
                        adjustCounter = adjustCounter + 1
                        time.sleep(1)

                    if adjustFlag == 0:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^校准6.86启动失败\")" % time1)
                        dox_command([20], err_inquiry_s14)  # 输出故障20
                        error_20 = [20]
                        automatic_cleaning_n = 37

                    if atcal == 0:
                        automatic_cleaning_n = 36
                    start_time = time.time()

                    # 每 5 秒读一个数，一共读 120 次
                    for i in range(125):
                        PHvalue = PH_num()
                        # 判断读数是否在范围内
                        if 536 <= int(PHvalue) <= 836:
                            # 如果在范围内，重置计时器
                            start_time = time.time()
                            break
                        else:
                            # 如果不在范围内，计算时间差
                            elapsed_time = time.time() - start_time
                            # 如果时间差超过 10 分钟，输出“校准失败”并结束循环
                            if elapsed_time > 600:
                                print("校准失败")
                                serial_val_modify(
                                    "warningpage.data0.insert(\"%s^6.86偏差过大\")" % time.strftime
                                    ('%H:%M:%S', time.localtime(time.time())))
                                dox_command([20], err_inquiry_s14)  # 输出故障20
                                error_20 = [20]
                                automatic_cleaning_n = 37
                                break
                        # 等待 5 秒
                        time.sleep(5)

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return

            if automatic_cleaning_n == 29:
                if special_jump_to_30 == 1:
                    automatic_cleaning_n = 30
                else:
                    print("自动清洗步骤29")
                    serial_val_modify("homepage.now_status.txt=\"自动校准6.86__29\"")
                    startTimestamp = time.time()
                    endTimeStamp = 0
                    measurementState = 0
                    PHdeque = deque(maxlen=15)
                    while len(PHdeque) < 15:
                        PHvalue = PH_num()
                        print("PH值1：", PHvalue)
                        PHdeque.append(PHvalue)
                        time.sleep(2)
                    while ((endTimeStamp - startTimestamp) <= 300) and (measurementState == 0):
                        # 从队列中去除最大值和最小值
                        PHdeque.remove(max(PHdeque))
                        PHdeque.remove(min(PHdeque))
                        # 比较
                        if abs(max(PHdeque) - min(PHdeque)) <= 10:
                            measurementState = 1
                        while len(PHdeque) < 15:  # 增加值
                            PHvalue = PH_num()
                            print("替换PH值：", PHvalue)
                            PHdeque.append(PHvalue)
                            time.sleep(2)
                        print("15个PH值", PHdeque)
                        endTimeStamp = time.time()
                        print("时间间隔：", (endTimeStamp - startTimestamp))

                    if (abs((sum(PHdeque) / len(PHdeque)) - 686) < 150) and measurementState == 1:
                        print("进入6.86校准确认")
                        adjustCounter = 0
                        adjustFlag = 0
                        while adjustCounter < 3 and adjustFlag == 0:
                            PH_status = PH_controller(6)
                            print(PH_status)
                            if PH_status == "0106000400ff884b":
                                print("ph6校准完成")
                                automatic_cleaning_n = 30
                                adjustFlag = 1
                            elif PH_status == "0186058263":
                                serial_val_modify(
                                    "warningpage.data0.insert(\"%s^6.86校准失败\")" % time.strftime
                                    ('%H:%M:%S', time.localtime(time.time())))
                                dox_command([20], err_inquiry_s14)  # 输出故障20
                                error_20 = [20]
                                automatic_cleaning_n = 37
                                adjustFlag = 1
                            adjustCounter = adjustCounter + 1
                            time.sleep(1)

                        if adjustFlag == 0:
                            serial_val_modify(
                                "warningpage.data0.insert(\"%s^6.86斜率过低\")" % time.strftime
                                ('%H:%M:%S', time.localtime(time.time())))
                            time.sleep(0.2)
                            serial_val_modify(
                                "warningpage.data0.insert(\"%s^建议更换电极\")" % time.strftime
                                ('%H:%M:%S', time.localtime(time.time())))
                            dox_command([20], err_inquiry_s14)  # 输出故障20
                            error_20 = [20]
                            automatic_cleaning_n = 37
                    else:
                        serial_val_modify(
                            "warningpage.data0.insert(\"%s^6.86数值不稳\")" % time.strftime
                            ('%H:%M:%S', time.localtime(time.time())))
                        time.sleep(0.2)
                        serial_val_modify(
                            "warningpage.data0.insert(\"%s^建议更换电极\")" % time.strftime
                            ('%H:%M:%S', time.localtime(time.time())))
                        dox_command([20], err_inquiry_s14)  # 输出故障20
                        error_20 = [20]
                        automatic_cleaning_n = 37

            if automatic_cleaning_n == 30:
                print("自动清洗步骤30")
                serial_val_modify("homepage.now_status.txt=\"自动校准4.00__30\"")
                cut_signal_21 = [21]
                dox_command([21, 19], err_inquiry_s14)
                injection_port(3)  # 切换阀复位,X-4指令
                time.sleep(5)
                if insert_database() == "ac03":  # 查询指令
                    insert_error_num(0, 's14')
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "ac03":  # 查询指令
                        insert_error_num(0, 's14')
                        pass  # 成功
                    else:
                        # time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        # serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                        insert_error_num(30, 's14')
                        automatic_cleaning_status = 0
                        return
                s14_t30 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t30 = s14_t30 + 1
                    time.sleep(1)
                    if s14_t30 >= 5:
                        automatic_cleaning_n = 31
                        break
                    if atcal == 0:
                        automatic_cleaning_n = 36
                        break

            if automatic_cleaning_n == 31:
                print("自动清洗步骤31")
                serial_val_modify("homepage.now_status.txt=\"自动校准4.00__31\"")
                dox_command([19, 7], err_inquiry_s14)  # 输出do28
                s14_t31 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        dox_command([19], err_inquiry_s14)
                        return
                    s14_t31 = s14_t31 + 1
                    time.sleep(1)
                    if s14_t31 >= Thc / 2:
                        automatic_cleaning_n = 32
                        break
                    if atcal == 0:
                        automatic_cleaning_n = 36
                        break

            if automatic_cleaning_n == 32:
                print("自动清洗步骤32")
                serial_val_modify("homepage.now_status.txt=\"自动校准4.00__32\"")
                dox_command([19], err_inquiry_s14)  # 复位do28
                s14_t32_1 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t32_1 = s14_t32_1 + 1
                    time.sleep(1)
                    if s14_t32_1 >= 5:
                        break
                    if atcal == 0:
                        automatic_cleaning_n = 36
                        break
                dox_command([19, 7], err_inquiry_s14)  # 输出do28
                s14_t310 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        dox_command([19], err_inquiry_s14)
                        return
                    s14_t310 = s14_t310 + 1
                    time.sleep(1)
                    if s14_t310 >= Thc / 2:
                        break
                    if atcal == 0:
                        automatic_cleaning_n = 36
                        break
                dox_command([19], err_inquiry_s14)  # 复位do24
                s14_t32 = 0
                while 1:
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return
                    s14_t32 = s14_t32 + 1
                    time.sleep(1)
                    if s14_t32 >= 29:
                        automatic_cleaning_n = 33
                        break
                    if atcal == 0:
                        automatic_cleaning_n = 36
                        break

            if automatic_cleaning_n == 33:
                print("自动清洗步骤33")
                serial_val_modify("homepage.now_status.txt=\"自动校准4.00__33\"")
                time.sleep(3)
                adjustCounter = 0
                adjustFlag = 0
                while adjustCounter < 3 and adjustFlag == 0:
                    status_now_4 = PH_enter_calibration(4)
                    print("33_enter_: " + status_now_4)
                    if status_now_4 == "0186030261":
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^校准4.01启动失败\")" % time1)
                        dox_command([20], err_inquiry_s14)  # 输出故障20
                        error_20 = [20]
                        automatic_cleaning_n = 37
                        adjustFlag = 1
                    elif status_now_4 == "010600020001e9ca":
                        automatic_cleaning_n = 34
                        adjustFlag = 1
                    adjustCounter = adjustCounter + 1
                    time.sleep(1)

                if adjustFlag == 0:
                    time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                    serial_val_modify("warningpage.data0.insert(\"%s^校准4.01启动失败\")" % time1)
                    dox_command([20], err_inquiry_s14)  # 输出故障20
                    error_20 = [20]
                    automatic_cleaning_n = 37

                if atcal == 0:
                    automatic_cleaning_n = 36

                start_time = time.time()
                # 每 5 秒读一个数，一共读 120 次
                for i in range(125):
                    PHvalue = PH_num()
                    # 判断读数是否在范围内
                    if 250 <= int(PHvalue) <= 550:
                        # 如果在范围内，重置计时器
                        start_time = time.time()
                        break
                    else:
                        # 如果不在范围内，计算时间差
                        elapsed_time = time.time() - start_time
                        # 如果时间差超过 10 分钟，输出“校准失败”并结束循环
                        if elapsed_time > 600:
                            print("校准失败")
                            serial_val_modify(
                                "warningpage.data0.insert(\"%s^4.00偏差过大\")" % time.strftime('%H:%M:%S',
                                                                                                time.localtime(
                                                                                                    time.time())))
                            dox_command([20], err_inquiry_s14)  # 输出故障20
                            error_20 = [20]
                            automatic_cleaning_n = 37
                            break
                    # 等待 5 秒
                    time.sleep(5)

            if mode == 0 or di_location[0:1][0] == "1":
                automatic_cleaning_status = 0
                return

            if automatic_cleaning_n == 34:
                print("自动清洗步骤34")
                serial_val_modify("homepage.now_status.txt=\"自动校准4.00__34\"")
                startTimestamp = time.time()
                endTimeStamp = 0
                measurementState = 0
                PHdeque = deque(maxlen=15)
                while len(PHdeque) < 15:
                    PHvalue = PH_num()
                    print("PH值1：", PHvalue)
                    PHdeque.append(PHvalue)
                    time.sleep(2)
                while ((endTimeStamp - startTimestamp) <= 300) and (measurementState == 0):
                    # 从队列中去除最大值和最小值
                    PHdeque.remove(max(PHdeque))
                    PHdeque.remove(min(PHdeque))
                    # 比较
                    if abs(max(PHdeque) - min(PHdeque)) <= 10:
                        measurementState = 1
                    while len(PHdeque) < 15:  # 增加值
                        PHvalue = PH_num()
                        print("替换PH值：", PHvalue)
                        PHdeque.append(PHvalue)
                        time.sleep(2)
                    print("15个PH值", PHdeque)
                    endTimeStamp = time.time()
                    print("时间间隔：", (endTimeStamp - startTimestamp))

                if (abs((sum(PHdeque) / len(PHdeque)) - 401) < 150) and measurementState == 1:
                    print("进入4.01校准确认")
                    adjustCounter = 0
                    adjustFlag = 0
                    while adjustCounter < 3 and adjustFlag == 0:
                        PH_status = PH_controller(4)
                        print(PH_status)
                        if PH_status == "0106000500ffd98b":
                            print("ph4校准完成")
                            automatic_cleaning_n = 35
                            adjustFlag = 1
                        elif PH_status == "0186058263":
                            serial_val_modify(
                                "warningpage.data0.insert(\"%s^4.01校准失败\")" % time.strftime('%H:%M:%S',
                                                                                                time.localtime(
                                                                                                    time.time())))
                            dox_command([20], err_inquiry_s14)  # 输出故障20
                            error_20 = [20]
                            automatic_cleaning_n = 37
                            adjustFlag = 1
                        adjustCounter = adjustCounter + 1
                        time.sleep(1)
                    if adjustFlag == 0:
                        serial_val_modify(
                            "warningpage.data0.insert(\"%s^4.01斜率过低\")" % time.strftime('%H:%M:%S',
                                                                                            time.localtime(
                                                                                                time.time())))
                        time.sleep(0.2)
                        serial_val_modify(
                            "warningpage.data0.insert(\"%s^建议更换电极\")" % time.strftime('%H:%M:%S',
                                                                                            time.localtime(
                                                                                                time.time())))
                        dox_command([20], err_inquiry_s14)  # 输出故障20
                        error_20 = [20]
                        automatic_cleaning_n = 37
                else:
                    serial_val_modify(
                        "warningpage.data0.insert(\"%s^4.01数值不稳\")" % time.strftime('%H:%M:%S',
                                                                                        time.localtime(
                                                                                            time.time())))
                    time.sleep(0.2)
                    serial_val_modify(
                        "warningpage.data0.insert(\"%s^建议更换电极\")" % time.strftime('%H:%M:%S',
                                                                                        time.localtime(
                                                                                            time.time())))
                    dox_command([20], err_inquiry_s14)  # 输出故障20
                    error_20 = [20]
                    automatic_cleaning_n = 37

            if automatic_cleaning_n == 35:
                print("自动清洗步骤35")
                time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                serial_val_modify("warningpage.data0.insert(\"%s^自动校准完成\")" % time1)
                C1 = 0
                sql_s14_time_storage("s14_9_t7", 0)
                sql_s14_time_storage("s14_24_C1", 0)
                # PH_choice(0)
                # time.sleep(3)  # 退出校准模式
                automatic_cleaning_n = 36

            if automatic_cleaning_n == 36:
                print("自动清洗步骤36")
                dox_command([19], err_inquiry_s14)
                serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
                serial_val_modify("homepage.now_status.txt=\"自动清洗中\"")
                is_require_s14 = 0
                s14_error = 0
                automatic_cleaning_n = 2

            if automatic_cleaning_n == 37:
                print("自动清洗步骤37")
                # injection_port_0()
                time.sleep(2)
                dox_command([0], err_inquiry_s14)  # 复位所有
                # PH_choice(0)
                # time.sleep(3)  # 退出校准模式
                serial_val_modify("homepage.now_status.txt=\"自动模式-校准失败\"")
                time.sleep(0.2)
                serial_val_modify("homepage.mod_error.txt=\"*详情查看报警信息页面\"")
                while 1:
                    time.sleep(1)
                    if mode == 0 or di_location[0:1][0] == "1":
                        automatic_cleaning_status = 0
                        return


def remote_maintenance_s15():
    global serial_txt, mode, di_location, remote_maintenance_status
    global flu, atcal, do_location, err_inquiry_s15, recharge_t, water_level_23, error_20, flu_t
    global i2c40, i2c41, i2c42, i2c43
    remote_maintenance_n = 0
    special_jump_to_13 = 0

    s15_error = 0
    try:
        s15_error = sql_query_DHT("s15_14t")
    except Exception as e:
        # 处理所有异常错误
        print('Error:', e)
        pass
    if s15_error > 0:  # 如果手动保养程序死机s15_error会大于0，所以需要跳到步骤13
        insert_error_num(13, 's15')

    while 1:
        if do_location[21:22][0] == "1" or do_location[22:23][0] == "1":
            remote_maintenance_n = 0
            return
        if remote_maintenance_n == 0:
            s15_t0 = 0  # 计时使用
            while s15_t0 <= 50:  # 时限5秒内可返回手动
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    return
                s15_t0 = s15_t0 + 1  # 没成功时间计数+1
                time.sleep(0.1)
            remote_maintenance_n = 1

        step_val = error_val('s15')
        if step_val != 0 and step_val < 5:  # 用于切换阀故障时跳转步骤
            remote_maintenance_n = step_num('s15')
            if remote_maintenance_n == 13:
                remote_maintenance_n = 9
                special_jump_to_13 = 1
        elif step_val >= 5:
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
            # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
            dox_command([20], err_inquiry_s3)  # 输出do3
            error_20 = [20]
            time.sleep(5)
            insert_error_num(0, 's15')
            return

        if di_location[5:6][0] == "1" and remote_maintenance_n != 12 and step_val == 0:
            remote_maintenance_n = 9

        if remote_maintenance_n == 1:
            print("远程保养步骤1")
            # serial_val_modify("homepage.now_status.txt=\"保养中\"")
            # serial_val_modify("homepage.now_status.txt=\"保养中\"")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤1\"")
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            serial_val_modify("warningpage.data0.insert(\"%s^自动模式\")" % time1)  # 待清除
            dox_command([21, 19], err_inquiry_s15)
            time.sleep(2)
            cut_signal(1)
            remote_maintenance_n = 2

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 2:
            print("远程保养步骤2")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤2\"")
            if flu == 0:
                remote_maintenance_n = 4
            elif flu == 1:
                remote_maintenance_n = 3

        if remote_maintenance_n == 3:
            print("远程保养步骤3")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤3\"")
            dox_command([21, 19, 4], err_inquiry_s15)
            s15_t3 = 0  # 计时使用
            while s15_t3 <= flu_t:  # 时停30秒
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    dox_command([21, 19], err_inquiry_s15)
                    return
                s15_t3 = s15_t3 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 4

        if remote_maintenance_n == 4:
            if flu == 0:
                dox_command([21, 19], err_inquiry_s15)
                print("远程保养步骤4")
                serial_val_modify("homepage.now_status.txt=\"自动保养步骤4\"")
                injection_port(4)  # 切换阀复位,X-4指令
                time.sleep(5)
                if insert_database() == "af06":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    insert_error_num(0, 's15')
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "af06":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                        insert_error_num(0, 's15')
                        pass  # 成功
                    else:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                        insert_error_num(4, 's15')
                        # dox_command([20], err_inquiry_s3)  # 输出do3
                        remote_maintenance_status = 0
                        return
            elif flu == 1:
                dox_command([21, 19, 4], err_inquiry_s15)
                print("远程保养步骤4")
                serial_val_modify("homepage.now_status.txt=\"自动保养步骤4\"")
                injection_port(4)  # 切换阀复位,X-4指令
                time.sleep(5)
                if insert_database() == "af06":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                    insert_error_num(0, 's15')
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "af06":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-4位置\"")
                        insert_error_num(0, 's15')
                        pass  # 成功
                    else:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                        insert_error_num(4, 's15')
                        # dox_command([20], err_inquiry_s3)  # 输出do3
                        remote_maintenance_status = 0
                        return
            remote_maintenance_n = 5

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            dox_command([21, 19], err_inquiry_s15)
            return
        if recharge_t == 0:
            io_restart()
            time.sleep(1)

        if remote_maintenance_n == 5:
            print("远程保养步骤5")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤5\"")
            if flu == 0:
                dox_command([19, 21, 3], err_inquiry_s15)
                time.sleep(2)
                remote_maintenance_n = 6
            elif flu == 1:
                dox_command([21, 19, 4, 3], err_inquiry_s15)
                time.sleep(2)
                remote_maintenance_n = 6

        if remote_maintenance_n == 6:
            print("远程保养步骤6")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤6\"")
            if flu == 0:
                dox_command([21, 19, 3, 2], err_inquiry_s15)
                s15_16 = 0
                while s15_16 <= 60:  # 时限一分钟
                    if di_location[5:6][0] == "1" or mode == 0:
                        s15_16 = 70  # 成功后跳出循环
                        remote_maintenance_n = 8
                        serial_val_modify("homepage.state_sw.val=1")
                        break
                    else:
                        s15_16 = s15_16 + 1  # 没成功时间计数+1
                        time.sleep(1)
                if s15_16 > 60 and di_location[5:6][0] == "0":
                    time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                    serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                    dox_command([20], err_inquiry_s3)  # 输出do3
                    error_20 = [20]
                    return
            elif flu == 1:
                dox_command([21, 19, 4, 3, 2], err_inquiry_s15)
                s15_16 = 0
                while s15_16 <= 60:  # 时限一分钟
                    if di_location[5:6][0] == "1" or mode == 0:
                        s15_16 = 70  # 成功后跳出循环
                        remote_maintenance_n = 8
                        serial_val_modify("homepage.state_sw.val=1")
                        break
                    else:
                        s15_16 = s15_16 + 1  # 没成功时间计数+1
                        time.sleep(1)
                if s15_16 > 60 and di_location[5:6][0] == "0":
                    time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                    serial_val_modify("warningpage.data0.insert(\"%s^检修故障\")" % time1)  # 存入故障信息
                    # # serial_val_modify("page0.now_position.txt=\"检修故障\"")
                    dox_command([20], err_inquiry_s3)  # 输出do3
                    error_20 = [20]
                    return

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            dox_command([21, 19], err_inquiry_s15)
            return

        if remote_maintenance_n == 8:
            print("远程保养步骤8")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤8\"")
            dox_command([21, 19, 3], err_inquiry_s15)
            s15_t8 = 0  # 计时使用
            while s15_t8 <= Tcx:
                if mode == 0 or di_location[0:1][0] == "0":
                    remote_maintenance_status = 0
                    dox_command([21, 19], err_inquiry_s15)
                    return
                s15_t8 = s15_t8 + 1  # 没成功时间计数+1
                time.sleep(1)
            remote_maintenance_n = 9

        if recharge_t == 0:
            io_restart()
            time.sleep(1)

        if remote_maintenance_n == 9:
            print("远程保养步骤9")
            serial_val_modify("homepage.now_status.txt=\"自动保养步骤9\"")
            if special_jump_to_13 == 1:
                remote_maintenance_n = 10
            else:
                dox_command([21, 19], err_inquiry_s15)
                injection_port(1)  # 切换阀复位,X-1指令
                time.sleep(5)
                if insert_database() == "aa01":  # 查询指令
                    # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                    insert_error_num(0, 's15')
                    pass  # 成功
                else:
                    time.sleep(3)
                    if insert_database() == "aa01":  # 查询指令
                        # # serial_val_modify("page0.now_position.txt=\"切换阀X-1位置\"")
                        insert_error_num(0, 's15')
                        pass  # 成功
                    else:
                        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
                        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
                        # # serial_val_modify("page0.now_position.txt=\"切换阀故障\"")
                        insert_error_num(9, 's15')
                        # dox_command([20], err_inquiry_s3)  # 输出do3
                        remote_maintenance_status = 0
                        return
                remote_maintenance_n = 10

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 10:
            if special_jump_to_13 == 1:
                remote_maintenance_n = 11
            else:
                print("远程保养步骤10")
                serial_val_modify("homepage.now_status.txt=\"自动保养步骤10\"")
                dox_command([21, 19, 5], err_inquiry_s15)
                s15_t10 = 0  # 计时使用
                while s15_t10 <= Tqx1:
                    if mode == 0 or di_location[0:1][0] == "0":
                        remote_maintenance_status = 0
                        dox_command([21, 19], err_inquiry_s15)
                        return
                    s15_t10 = s15_t10 + 1  # 没成功时间计数+1
                    time.sleep(1)
                remote_maintenance_n = 11

        if remote_maintenance_n == 11:
            if special_jump_to_13 == 1:
                remote_maintenance_n = 12
            else:
                print("远程保养步骤11")
                serial_val_modify("homepage.now_status.txt=\"自动保养步骤11__电极浸泡中\"")
                dox_command([21, 19], err_inquiry_s15)
                s15_t11 = 0  # 计时使用
                s15_io_res_11 = 0
                while s15_t11 <= Tqx2:
                    s15_io_res_11 = s15_io_res_11 + 1
                    if s15_io_res_11 >= 60 and recharge_t == 0:
                        s15_io_res_11 = 0
                        io_restart()  # 复位下dido的问询
                        time.sleep(1)
                        dox_command([21, 19], err_inquiry_s15)
                    if mode == 0 or di_location[0:1][0] == "0":
                        remote_maintenance_status = 0
                        return
                    s15_t11 = s15_t11 + 1  # 没成功时间计数+1
                    time.sleep(1)
                remote_maintenance_n = 12

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 12:
            print("远程保养步骤12")
            if special_jump_to_13 == 1:
                remote_maintenance_n = 13
            else:
                serial_val_modify("homepage.now_status.txt=\"自动保养步骤12\"")
                time.sleep(1)
                # while water_level_23 == [23]:
                #     time.sleep(1)
                #     if mode == 0 or di_location[0:1][0] == "0":
                #         remote_maintenance_status = 0
                #         return
                dox_command([21, 19, 6], err_inquiry_s15)
                s15_t12 = 0  # 计时使用
                s15_io_res_12 = 0
                while s15_t12 <= Tbh:
                    if mode == 0 or di_location[0:1][0] == "0":
                        remote_maintenance_status = 0
                        dox_command([21, 19], err_inquiry_s15)
                        return
                    s15_io_res_12 = s15_io_res_12 + 1
                    if s15_io_res_12 >= 60 and recharge_t == 0:
                        s15_io_res_12 = 0
                        io_restart()  # 复位下dido的问询
                        time.sleep(1)
                    s15_t12 = s15_t12 + 1  # 没成功时间计数+1
                    time.sleep(1)
                remote_maintenance_n = 13

        if remote_maintenance_n == 13:
            if special_jump_to_13 == 1:
                remote_maintenance_n = 14
                insert_error_num(0, 's15')
            else:
                print("远程保养步骤13")
                serial_val_modify("homepage.now_status.txt=\"自动保养步骤13\"")
                dox_command([21, 19], err_inquiry_s15)
                # injection_port_0()
                remote_maintenance_n = 14

        if mode == 0 or di_location[0:1][0] == "0":
            remote_maintenance_status = 0
            return

        if remote_maintenance_n == 14:
            print("远程保养步骤14")
            sql_DHT_del("jump_9")
            special_jump_to_13 = 0
            serial_val_modify("homepage.now_status.txt=\"自动保养__电极养护中\"")
            s15_t14 = 0  # 计时使用
            if s15_error > 0:
                s15_t14 = s15_error
            s15_io_res_14 = 0
            while s15_t14 <= Tby:
                if mode == 0 or di_location[0:1][0] == "0":
                    sql_DHT_del("s15_14t")
                    sql_DHT("jump_9", 1)  # 跳出远程保养长延时的时候也跳出看门狗
                    remote_maintenance_status = 0
                    return
                s15_io_res_14 = s15_io_res_14 + 1
                if s15_io_res_14 >= 60 and recharge_t == 0:
                    s15_io_res_14 = 0
                    sql_DHT("s15_14t", s15_t14)  # 不断更新DHT表中的数据,喂看门狗
                    io_restart()  # 复位下dido的问询
                    time.sleep(1)
                    dox_command([21, 19], err_inquiry_s15)
                    s15_t14 = s15_t14 + 3  # 手动补充时间
                s15_t14 = s15_t14 + 1  # 没成功时间计数+1
                time.sleep(1)
                if do_location[22:23][0] == "1":
                    remote_maintenance_n = 0
                    remote_maintenance_status = 0
                    sql_DHT("jump_9", 1)
                    insert_error_num(13, 's15')
                    return
            sql_DHT_del("s15_14t")
            sql_DHT("jump_9", 1)  # 跳出远程保养长延时的时候也跳出看门狗
            remote_maintenance_n = 12


def cut_signal(num):
    global err_inquiry_s14_21, cut_signal_21, s2_state_21
    if num == 1:
        dox_command([21], err_inquiry_s14_21)
        cut_signal_21 = [21]
        s2_state_21 = [21]
    elif num == 0:
        cut_signal_21 = [0]
        s2_state_21 = [0]
        dox_command([0], err_inquiry_s14_21)


def control():  # 主控制程序，用于手动，自动保养和自动清洗的切换
    global mode, manual_status, remote_maintenance_status, automatic_cleaning_status
    global do_location, di_location
    global is_require_s14  # 从手动、自动保养切至自动清洗后，须立刻进行一次清洗校准
    global s14_timing_judgement  # 自动清洗退出后，判断是否要在手动中计时
    global flu, atcal, serial_txt
    is_require_s14 = 1
    manual_status = 0  # 手动模式开启记录
    remote_maintenance_status = 0  # 自动保养开启记录
    automatic_cleaning_status = 0  # 自动清洗开启记录
    err_status = 0
    s14_timing_judgement = 0
    time.sleep(2)
    while 1:
        time.sleep(0.2)
        if (mode == 1 and di_location[0:1][0] == "1" and do_location[22:23][0] == "1") or (
                mode == 1 and di_location[0:1][0] == "1" and do_location[21:22][0] == "1") or (
                mode == 1 and di_location[0:1][0] == "0" and do_location[21:22][0] == "1") or (
                mode == 1 and di_location[0:1][0] == "0" and do_location[23:24][0] == "1" and atcal == 1):
            print("手动需补液")
            manual_status = 1
            is_require_s14 = 1
            s14_timing_judgement = 1
            sql_insert_mode(0)
            mode = 0
            time.sleep(2)
            print("mode", mode)
            serial_val_modify("homepage.mod_sw.val=0")
            time.sleep(0.2)
            serial_val_modify("homepage.now_status.txt=\"手动模式-请补液完成后切回自动\"")
            time.sleep(0.2)
            serial_val_modify("homepage.mod_error.txt=\"*补液之外的操作会复位自动程序\"")
            manual_control()
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            # if (di_location[0:1][0] == "1" and do_location[22:23][0] == "1") or (
            #         di_location[0:1][0] == "1" and do_location[21:22][0] == "1"): 液体不足情况标注
            sql_insert_mode(1)
            mode = 1
            time.sleep(0.2)
            print("手动需要补液重启")
            automatic_cleaning_status = 0
            remote_maintenance_status = 0
            #
            # if (di_location[0:1][0] == "0" and do_location[21:22][0] == "1") or (
            #         di_location[0:1][0] == "0" and do_location[23:24][0] == "1" and atcal == 1):
            #     sql_insert_mode(1)

        if mode == 0 and manual_status == 0:
            serial_val_modify("homepage.now_status.txt=\"手动模式开启完成\"")
            serial_val_modify("homepage.mod_error.txt=\" \"")
            manual_status = 1
            err_status = 0
            print("手动")
            # injection_port_0()
            # serial_val_modify("homepage.now_status.txt=\"设备开启完成\"")
            manual_control()
            automatic_cleaning_status = 0
            remote_maintenance_status = 0
            is_require_s14 = 1

        if serial_txt == "ffff00":
            mode = 1  # 开启远程模式
            print("到了远程")
            print("mode:", mode)
            print("di_location[0:1][0]:", di_location[0:1][0])
            print("automatic_cleaning_status:", automatic_cleaning_status)
            print("atcal:", atcal)
            print("do_location[19:20][0]:", do_location[19:20][0])
            print("do_location[21:22][0]:", do_location[21:22][0])
            print("do_location[23:24][0]:", do_location[23:24][0])
            sql_insert_mode(1)
            serial_txt = 0

        if serial_txt == "ffff01":
            mode = 0  # 开启就地模式
            print("到了就地")
            sql_insert_mode(0)
            serial_txt = 0

        if (mode == 1 and di_location[0:1][0] == "0" and automatic_cleaning_status == 0 and atcal == 1
            and do_location[19:20][0] == "0" and do_location[21:22][0] == "0" and do_location[23:24][0] == "0") \
                or (mode == 1 and di_location[0:1][0] == "0" and automatic_cleaning_status == 0 and atcal == 0
                    and do_location[19:20][0] == "0" and do_location[21:22][0] == "0"):
            automatic_cleaning_status = 1
            print("自动清洗")
            # injection_port_0()
            serial_val_modify("homepage.now_status.txt=\"自动清洗模式\"")
            serial_val_modify("homepage.mod_error.txt=\" \"")
            automatic_cleaning_s14()
            is_require_s14 = 0
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            #  # serial_val_modify("page0.now_status.txt=\"重启中\"")
            # restart_exe()

        if mode == 1 and di_location[0:1][0] == "1" and remote_maintenance_status == 0 \
                and do_location[19:20][0] == "0" and do_location[21:22][0] == "0" and do_location[22:23][0] == "0":
            remote_maintenance_status = 1
            print("自动保养")
            # injection_port_0()
            serial_val_modify("homepage.now_status.txt=\"自动保养模式\"")
            serial_val_modify("homepage.mod_error.txt=\" \"")
            remote_maintenance_s15()
            is_require_s14 = 1
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            #  # serial_val_modify("page0.now_status.txt=\"重启中\"")
            # restart_exe()

        if mode == 1 and do_location[19:20][0] == "1" and err_status == 0:
            is_require_s14 = 1
            err_status = 1
            print("设备故障")
            dox_command([20], err_inquiry_21_24)  # 输出do3
            serial_val_modify("homepage.now_status.txt=\"设备故障\"")


def sql_insert_parameter(tqx1, tqx2, tby, txh, tco, thc):  # 储存当前系统参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT system_parameter(date,tqx1,tqx2,tby,txh,tco,thc) VALUE(%s,%s,%s,%s,%s,%s,%s)",
                    [(time1, tqx1, tqx2, tby, txh, tco, thc)])

    cur.execute("SELECT min(id) FROM system_parameter ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM system_parameter ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM system_parameter " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def sql_insert_parameter_1(tbh_t, tcx_t, flu_t_t, atcal_t_t, flu_val, atcal_val):  # 储存当前系统参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT system_parameter_1(date,tbh, tcx, flu_t, atcal_t, flu, atcal) VALUE(%s,%s,%s,%s,%s,%s,%s)",
                    [(time1, tbh_t, tcx_t, flu_t_t, atcal_t_t, flu_val, atcal_val)])
    cur.execute("SELECT min(id) FROM system_parameter_1 ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM system_parameter_1 ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM system_parameter_1 " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def sql_insert_parameter_2(tup, tdp, tuf, tdf, huf, hdf):  # 储存当前系统参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT system_parameter_2(date,tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, "
                    "hum_down_fan) VALUE(%s,%s,%s,%s,%s,%s,%s)",
                    [(time1, tup, tdp, tuf, tdf, huf, hdf)])
    cur.execute("SELECT min(id) FROM system_parameter_2 ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM system_parameter_2 ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM system_parameter_2 " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def sql_s14_time_storage(name, time_storage):  # 储存当前s14的时间参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT time_storage(time ,name, time_storage) VALUE(%s,%s,%s)",
                    [(time1, name, time_storage)])
    cur.execute("SELECT min(id) FROM time_storage ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM time_storage ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM time_storage " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def sql_DHT(name, time_storage):  # 储存名字和时间到DHT表中
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT DHT(time ,address, temperature) VALUE(%s,%s,%s)",
                    [(time1, name, time_storage)])
    # cur.execute("SELECT min(id) FROM DHT ")
    # min_mode = int(cur.fetchone()[0])
    # cur.execute("SELECT max(id) FROM DHT ")
    # max_mode = int(cur.fetchone()[0])
    # if (max_mode - min_mode) >= 1000:
    #     sql2 = ("delete FROM DHT " + "WHERE id <= %s " % (max_mode - 1000) + " and id >= %s" % min_mode)
    #     cur.execute(sql2)
    con.commit()
    cur.close()
    return


def sql_DHT_del(txt):  # 删除DHT表中address列名字是txt的行
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    sql = "DELETE FROM DHT WHERE address = %s"
    cur.execute(sql, txt)
    con.commit()
    cur.close()
    return


def sql_query_DHT(txt):  # 读取DHT表中address列中名称为txt的最后一行的temperature
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    sql = "SELECT temperature FROM DHT WHERE address = %s ORDER BY id DESC LIMIT 1"
    cur.execute(sql, txt)
    s14_9_t7 = int(cur.fetchone()[0])
    cur.close()
    return s14_9_t7


def sql_query_s14_9_t7():  # 读取time_storage表中储存的s14步骤9延时时间
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select time_storage from time_storage where name ='s14_9_t7' order by id desc limit 1")
    s14_9_t7 = int(cur.fetchone()[0])
    cur.close()
    return s14_9_t7


def sql_query_s14_24_C1():  # 读取time_storage表中储存的s14步骤24延时时间
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select time_storage from time_storage where name ='s14_24_C1' order by id desc limit 1")
    s14_24_C1 = int(cur.fetchone()[0])
    cur.close()
    return s14_24_C1


def sql_query_s14_switching():  # 读取time_storage表中储存的是运行后校准还是校准后运行
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select time_storage from time_storage where name ='Calibration_switching' order by id desc limit 1")
    s14_switching = int(cur.fetchone()[0])
    cur.close()
    return s14_switching


def sql_system_parameter():  # 查询当前系统参数
    global Tqx1, Tqx2, Tby, Txh, Tco, Thc
    global tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan
    global Tbh, Tcx, flu_t, atcal_t, flu, atcal, Tco_minute, Txh_minute, Tby_minute
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    cur.execute("select tqx1 from system_parameter order by id desc limit 1")
    Tqx1 = int(cur.fetchone()[0])
    cur.execute("select tqx2 from system_parameter order by id desc limit 1")
    Tqx2 = int(cur.fetchone()[0])
    cur.execute("select tby from system_parameter order by id desc limit 1")
    Tby = int(cur.fetchone()[0])
    cur.execute("select txh from system_parameter order by id desc limit 1")
    Txh = int(cur.fetchone()[0])
    cur.execute("select tco from system_parameter order by id desc limit 1")
    Tco = int(cur.fetchone()[0])
    cur.execute("select thc from system_parameter order by id desc limit 1")
    Thc = int(cur.fetchone()[0])
    cur.execute("select tem_up_heat from system_parameter_2 order by id desc limit 1")
    tem_up_heat = int(cur.fetchone()[0])
    cur.execute("select tem_down_heat from system_parameter_2 order by id desc limit 1")
    tem_down_heat = int(cur.fetchone()[0])
    cur.execute("select tem_up_fan from system_parameter_2 order by id desc limit 1")
    tem_up_fan = int(cur.fetchone()[0])
    cur.execute("select tem_down_fan from system_parameter_2 order by id desc limit 1")
    tem_down_fan = int(cur.fetchone()[0])
    cur.execute("select hum_up_fan from system_parameter_2 order by id desc limit 1")
    hum_up_fan = int(cur.fetchone()[0])
    cur.execute("select hum_down_fan from system_parameter_2 order by id desc limit 1")
    hum_down_fan = int(cur.fetchone()[0])
    cur.execute("select tbh from system_parameter_1 order by id desc limit 1")
    Tbh = int(cur.fetchone()[0])
    cur.execute("select tcx from system_parameter_1 order by id desc limit 1")
    Tcx = int(cur.fetchone()[0])
    cur.execute("select flu_t from system_parameter_1 order by id desc limit 1")
    flu_t = int(cur.fetchone()[0])
    cur.execute("select atcal_t from system_parameter_1 order by id desc limit 1")
    atcal_t = int(cur.fetchone()[0])
    cur.execute("select flu from system_parameter_1 order by id desc limit 1")
    flu = int(cur.fetchone()[0])
    cur.execute("select atcal from system_parameter_1 order by id desc limit 1")
    atcal = int(cur.fetchone()[0])

    cur.execute("select val from minute_table where address ='online_minute' order by id desc limit 1")
    Txh_minute = int(cur.fetchone()[0])
    cur.execute("select val from minute_table where address ='cavity_minute' order by id desc limit 1")
    Tco_minute = int(cur.fetchone()[0])
    cur.execute("select val from minute_table where address ='pro_minute' order by id desc limit 1")
    Tby_minute = int(cur.fetchone()[0])
    cur.close()
    return


def sql_insert_minute(name, time_storage):  # 储存分钟类型的时间参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT minute_table(time ,address, val) VALUE(%s,%s,%s)",
                    [(time1, name, time_storage)])
    cur.execute("SELECT min(id) FROM minute_table ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM minute_table ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM minute_table " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)
    con.commit()
    cur.close()
    return


def system_parameter():
    global Tqx1, Tqx2, Tby, Txh, serial_txt, Tco, Thc
    global serial_time, serial_txt, err_inquiry_all
    global tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan
    global Tbh, Tcx, flu_t, atcal_t, flu, atcal, Tco_minute, Txh_minute, Tby_minute
    system_parameter_count = 0
    year = time.strftime("%Y")
    month = time.strftime("%m")
    day = time.strftime("%d")
    hour = time.strftime("%H")
    minute = time.strftime("%M")
    second = time.strftime("%S")
    while 1:
        time.sleep(0.2)
        if system_parameter_count == 0:
            system_parameter_count = 1
            sql_system_parameter()
            time.sleep(1)
            clean_time = int(Txh / 86400)
            lnjection_time = int(Tqx1)
            teardown_time = int(Tby / 86400)
            soaking_time = int(Tqx2 / 60)
            cavity_time = int(Tco / 86400)
            corr_h2o_time = int(Thc)
            Tco_minute_time = int(Tco_minute / 60)
            Txh_minute_time = int(Txh_minute / 60)
            Tby_minute_time = int(Tby_minute / 60)
            serial_val_modify("runpage.cavity_minute.val=%s" % Tco_minute_time)
            serial_val_modify("runpage.online_minte.val=%s" % Txh_minute_time)
            serial_val_modify("runpage.pro_minute.val=%s" % Tby_minute_time)
            serial_val_modify("runpage.online_val.val=%s" % clean_time)
            serial_val_modify("runpage.pump_4_val.val=%s" % lnjection_time)
            serial_val_modify("runpage.pro_pump_val.val=%s" % teardown_time)
            serial_val_modify("runpage.pole_pump_val.val=%s" % soaking_time)
            serial_val_modify("runpage.cavity_val.val=%s" % cavity_time)
            serial_val_modify("runpage.pump_6_val.val=%s" % corr_h2o_time)
            serial_val_modify("more_2.heat_up_val.val=%s" % tem_up_heat)
            serial_val_modify("more_2.heat_down_val.val=%s" % tem_down_heat)
            serial_val_modify("more_2.tem_up_val.val=%s" % tem_up_fan)
            serial_val_modify("more_2.tem_down_val.val=%s" % tem_down_fan)
            serial_val_modify("more_2.hum_up_val.val=%s" % hum_up_fan)
            serial_val_modify("more_2.hum_down_val.val=%s" % hum_down_fan)
            serial_val_modify("more_1.protect_val.val=%s" % Tbh)
            serial_val_modify("more_1.pole_wash_val.val=%s" % Tcx)
            serial_val_modify("more_1.rack_wash_val.val=%s" % flu_t)
            serial_val_modify("more_1.corr_ack_val.val=%s" % atcal_t)
            serial_val_modify("more_1.bracket_wash.val=%s" % flu)
            serial_val_modify("more_1.correct_ack.val=%s" % atcal)
            print("时间比对成功")

        if str(serial_txt)[0:4] == "1401":
            Txh_minute_s = str(serial_txt[4:6])  # 自动清洗间隔
            Txh_minute = (int(Txh_minute_s, 16)) * 60
            sql_insert_minute("online_minute", Txh_minute)
            serial_txt = 0

        if str(serial_txt)[0:4] == "1402":
            Tco_minute_s = str(serial_txt[4:6])  # 自动校准间隔
            Tco_minute = (int(Tco_minute_s, 16)) * 60
            sql_insert_minute("cavity_minute", Tco_minute)
            serial_txt = 0

        if str(serial_txt)[0:4] == "1403":
            Tby_minute_s = str(serial_txt[4:6])  # 保护液输送间隔
            Tby_minute = (int(Tby_minute_s, 16)) * 60
            sql_insert_minute("pro_minute", Tby_minute)
            serial_txt = 0

        if str(serial_txt)[0:4] == "0201":
            Txh_s = str(serial_txt[4:6])  # 自动清洗间隔
            Txh = ((int(Txh_s, 16)) * 3600 * 24) + Txh_minute
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0202":
            Tco_s = str(serial_txt[4:6])  # 自动校准间隔
            Tco = ((int(Tco_s, 16)) * 3600 * 24) + Tco_minute
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0203":
            Tby_s = str(serial_txt[4:6])  # 保护液输送间隔
            Tby = ((int(Tby_s, 16)) * 3600 * 24) + Tby_minute
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0204":
            Tqx2_s = str(serial_txt[4:6])  # 电极浸泡时间
            Tqx2 = (int(Tqx2_s, 16)) * 60
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0205":
            Tqx1_s = str(serial_txt[4:6])  # 清洗液输送时间
            Tqx1 = (int(Tqx1_s, 16))
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "0206":
            Thc_s = str(serial_txt[4:6])  # 缓冲液输送时间
            Thc = (int(Thc_s, 16))
            serial_txt = 0
            sql_insert_parameter(Tqx1, Tqx2, Tby, Txh, Tco, Thc)

        if str(serial_txt)[0:4] == "1001":
            Tbh_s = str(serial_txt[4:6])  # 输保护液时间
            Tbh = (int(Tbh_s, 16))
            serial_txt = 0
            sql_insert_parameter_1(Tbh, Tcx, flu_t, atcal_t, flu, atcal)

        if str(serial_txt)[0:4] == "1002":
            Tcx_s = str(serial_txt[4:6])  # 电极冲洗时间
            Tcx = (int(Tcx_s, 16))
            serial_txt = 0
            sql_insert_parameter_1(Tbh, Tcx, flu_t, atcal_t, flu, atcal)

        if str(serial_txt)[0:4] == "1003":
            flu_t_s = str(serial_txt[4:6])  # 支架冲洗时间
            flu_t = (int(flu_t_s, 16))
            serial_txt = 0
            sql_insert_parameter_1(Tbh, Tcx, flu_t, atcal_t, flu, atcal)

        if str(serial_txt)[0:4] == "1004":
            atcal_t_s = str(serial_txt[4:6])  # 校准确认时间
            atcal_t = (int(atcal_t_s, 16))
            serial_txt = 0
            sql_insert_parameter_1(Tbh, Tcx, flu_t, atcal_t, flu, atcal)

        if str(serial_txt)[0:4] == "1005":
            flu_s = str(serial_txt[4:6])  # 校准确认时间
            flu = (int(flu_s, 16))
            serial_txt = 0
            sql_insert_parameter_1(Tbh, Tcx, flu_t, atcal_t, flu, atcal)

        if str(serial_txt)[0:4] == "1006":
            atcal_s = str(serial_txt[4:6])  # 校准确认时间
            atcal = (int(atcal_s, 16))
            serial_txt = 0
            sql_insert_parameter_1(Tbh, Tcx, flu_t, atcal_t, flu, atcal)

        if str(serial_txt)[0:4] == "0901":
            tem_up_heat_s = str(serial_txt[4:6])  # 加热开启温度
            tem_up_heat = (int(tem_up_heat_s, 16))
            serial_txt = 0
            sql_insert_parameter_2(tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan)

        if str(serial_txt)[0:4] == "0902":
            tem_down_heat_s = str(serial_txt[4:6])  # 加热关闭温度
            tem_down_heat = (int(tem_down_heat_s, 16))
            serial_txt = 0
            sql_insert_parameter_2(tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan)

        if str(serial_txt)[0:4] == "0903":
            tem_up_fan_s = str(serial_txt[4:6])  # 风扇开启温度
            tem_up_fan = (int(tem_up_fan_s, 16))
            serial_txt = 0
            sql_insert_parameter_2(tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan)

        if str(serial_txt)[0:4] == "0904":
            tem_down_fan_s = str(serial_txt[4:6])  # 风扇关闭温度
            tem_down_fan = (int(tem_down_fan_s, 16))
            serial_txt = 0
            sql_insert_parameter_2(tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan)

        if str(serial_txt)[0:4] == "0905":
            hum_up_fan_s = str(serial_txt[4:6])  # 风扇开启湿度
            hum_up_fan = (int(hum_up_fan_s, 16))
            serial_txt = 0
            sql_insert_parameter_2(tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan)

        if str(serial_txt)[0:4] == "0906":
            hum_down_fan_s = str(serial_txt[4:6])  # 风扇关闭湿度
            hum_down_fan = (int(hum_down_fan_s, 16))
            serial_txt = 0
            sql_insert_parameter_2(tem_up_heat, tem_down_heat, tem_up_fan, tem_down_fan, hum_up_fan, hum_down_fan)

        if str(serial_time)[0:4] == "0101":
            print('0101的', serial_time)
            year_s = serial_time[6:8] + serial_time[4:6]  # 年
            month_s = serial_time[8:10]  # 月
            year = (int(year_s, 16))
            month = (int(month_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0102":
            print('0102的', serial_time)
            day_s = serial_time[4:6]  # 日
            hour_s = serial_time[8:10]  # 小时
            day = (int(day_s, 16))
            hour = (int(hour_s, 16))
            serial_time = 0

        if str(serial_time)[0:4] == "0103":
            print('0103的', serial_time)
            minute_s = serial_time[4:6]  # 分钟
            second_s = serial_time[8:10]  # 秒
            minute = (int(minute_s, 16))
            second = (int(second_s, 16))

            if month < 10:
                month = '0' + str(month)
            else:
                month = str(month)

            if hour < 10:
                hour = '0' + str(hour)
            else:
                hour = str(hour)

            if minute < 10:
                minute = '0' + str(minute)
            else:
                minute = str(minute)

            if second < 10:
                second = '0' + str(second)
            else:
                second = str(second)
            time_date = '"' + str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(
                minute) + ":" + str(second) + '"'
            time_update = "sudo date  --s=" + time_date
            os.system(time_update)
            time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
            # serial_val_modify("warningpage.data0.insert(\"%s^时间设置成功\")" % time1)  # 存入时间设置信息
            serial_time = 0
        #
        # if str(serial_time)[0:4] == "0104":
        #     hour_s = serial_time[4:6]  # 小时
        #     hour = (int(hour_s, 16))
        #     serial_time = 0
        #
        # if str(serial_time)[0:4] == "0105":
        #     minute_s = serial_time[4:6]  # 分钟
        #     minute = (int(minute_s, 16))
        #     serial_time = 0

        # if str(serial_time)[0:4] == "0106":
        #     time_date = str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(minute) + ":00"
        #     time_update = "sudo date  --s=" + time_date
        #     print(time_update)
        #     serial_time = 0
        #     os.system(time_update)

        if str(serial_txt)[0:4] == "6666":  # 系统复位按钮
            print("重启")
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            serial_val_modify("homepage.now_status.txt=\"重启中\"")
            #  # serial_val_modify("page0.now_status.txt=\"重启中\"")
            # dox_command(err_inquiry_all, [0])# 暂时修改为全复位
            dox_command([0], [0])  # 暂时修改为全复位
            insert_error_num(0, 's3')
            insert_error_num(0, 's14')
            insert_error_num(0, 's15')
            restart_exe()
        # 用于判断校准后运行为0，还是运行后校准为1
        if str(serial_txt)[0:4] == "6600":  # 运行后校准
            sql_s14_time_storage("Calibration_switching", 1)
            serial_txt = 0
        if str(serial_txt)[0:4] == "6601":  # 校准后运行
            sql_s14_time_storage("Calibration_switching", 0)
            serial_txt = 0


def recharge():  # 补充液体
    global serial_txt, recharge_t, status_restart_timing
    while 1:
        time.sleep(0.2)
        if serial_txt == "080101" and status_restart_timing == 0:
            recharge_t = 1
            replenish_clean_supply_s10()  # 补充清洗液
            recharge_t = 0
            serial_txt = 0

        if serial_txt == "080301" and status_restart_timing == 0:
            recharge_t = 1
            replenish_protect_supply_s11()  # 补充保护液
            recharge_t = 0
            serial_txt = 0

        if serial_txt == "080201" and status_restart_timing == 0:
            recharge_t = 1
            replenish_rehydration_4_s12()  # 补充缓冲液4
            recharge_t = 0
            serial_txt = 0

        if serial_txt == "080401" and status_restart_timing == 0:
            recharge_t = 1
            replenish_rehydration_6_s13()  # 补充缓冲液6
            recharge_t = 0
            serial_txt = 0


def sql_test(message):  # 储存当前s14的时间参数
    con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany("INSERT test(time ,message) VALUE(%s,%s)",
                    [(time1, message)])
    con.commit()
    cur.close()


def sql_test_del_main_restart():  # 删除test表中的main_restart
    con_del_main_start = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur_del_main_start = con_del_main_start.cursor()
    sql2 = "DELETE FROM test WHERE message = 'main_restart'"
    cur_del_main_start.execute(sql2)
    con_del_main_start.commit()
    cur_del_main_start.close()


threads = []
t1 = threading.Thread(target=serial_read)
threads.append(t1)

t2 = threading.Thread(target=recharge)
threads.append(t2)

t3 = threading.Thread(target=control)
threads.append(t3)

t4 = threading.Thread(target=water_level_monitoring)
threads.append(t4)

t5 = threading.Thread(target=system_parameter)
threads.append(t5)

t6 = threading.Thread(target=io_status)
threads.append(t6)

t7 = threading.Thread(target=do_not_reset)
threads.append(t7)

t8 = threading.Thread(target=DHT11)
threads.append(t8)

if __name__ == "__main__":
    # 明日还需设置时间，建立时间表
    # 程序状态
    s4_state, s5_state, s6_state, s7_state, s8_state = [0], [0], [0], [0], [0]
    s9_state, s10_state, s11_state, s12_state, s13_state = [0], [0], [0], [0], [0]
    s1_state, s2_state = [0], [0]
    s2_state_21 = [0]
    err_inquiry_s1_21 = [0]
    DHT11_state_13, DHT11_state_14, DHT11_state_15, DHT11_state_16, DHT11_state_17 = [0], [0], [0], [0], [0]
    cut_signal_21 = [0]  # 切除信号21专用
    water_level_22, water_level_23, water_level_24 = [0], [0], [0]
    error_20 = [0]
    err_inquiry_all = [0]
    recharge_t = 0  # 用于补充液体时，can通讯不复位，不复位那部分暂时还没写
    i2c40, i2c41, i2c42, i2c43 = 0, 0, 0, 0  # 初始化四个液位
    i2c40_t, i2c41_t, i2c42_t, i2c43_t = 0, 0, 0, 0  # 低液位报警，报警时为1
    YW40L = 60  # 清洗液最低水位
    YW41L = 60  # 保护液最低水位
    YW42L = 60  # 缓冲液4最低水位
    YW43L = 60  # 缓冲液6最低水位
    YW40H = 200  # 清洗液最高水位
    YW41H = 200  # 保护液最高水位
    YW42H = 200  # 缓冲液4最高水位
    YW43H = 200  # 缓冲液6最高水位
    err_inquiry_s1 = ['0']  # s1所用的不可复位do
    err_inquiry_s2 = ['0']  # s2所用的不可复位do
    err_inquiry_s3 = ['0']  # s3所用的不可复位do
    err_inquiry_s3_exit = ['0']  # s3退出所用的不可复位do
    err_inquiry_s3_exit_1_6 = ['0']  # s3,1-6步骤退出所用的不可复位do
    err_inquiry_s4_s10 = ['0']  # s4到s10所用的不可复位do
    err_inquiry_s10_s13 = ['0']  # s10到s13所用的不可复位do
    err_inquiry_s14_21 = ['0']
    err_inquiry_s01 = ['0']  # 用于手动状态下的首次复位
    err_inquiry_s14 = ['0']  # 用于自动动状态下的首次复位,还有s14
    err_inquiry_s15 = ['0']  # s15
    err_inquiry_22 = ['0']  # 清洗液报警专用
    err_inquiry_23 = ['0']  # 保护液报警专用
    err_inquiry_24 = ['0']  # 缓冲液报警专用
    err_inquiry_13 = ['0']  # 温湿度报警专用
    err_inquiry_14_16 = ['0']  # 温湿度报警专用
    err_inquiry_27_30 = ['0']  # 补液专用
    err_inquiry_21_24 = ['0']  # 报警专用
    manual_status = 0  # 手动模式开启记录
    remote_maintenance_status = 0  # 自动保养开启记录
    automatic_cleaning_status = 0  # 自动清洗开启记录
    maintenance_status = 0  # 保养状态
    do_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    di_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    # do和di状态的首次状态预置值
    serial_txt = 0  # 串口所读文本
    serial_time = 0  # 标定时间所用
    mode = 3  # 3是没有选择模式，0是手动模式，1是自动模式
    data_do, data_di = 0, 0  # 用于判断do和di
    var1, var2 = '00000000000000000000000000000000', '00000000000000000000000000000000'  # 用于判断do和di
    status_restart_timing = 1  # 用于重启can通讯的预置值，1为开启重启
    io_status_quit = 1  # 用于重启can通讯的预置值，1为开启重启
    Tqx1 = 5  # 清洗液输送时间，单位：秒
    Tqx2 = 60  # 清洗液浸泡时间，5分钟,换算秒为300秒
    Tby = 1296000  # 再次保养间隔时间，15天，换算秒为1296000秒
    Tby_minute = 0  # 再次保养间隔时间的补充，单位为分钟
    Thc = 15  # 缓冲液输送时间，单位：秒
    Txh = 3600  # 清洗液大循环时间，36小时，换算秒为129600秒
    Txh_minute = 0  # 清洗液大循环时间的补充，单位为分钟
    Tco = 1296000  # 自动校准间隔
    Tco_minute = 0  # 自动校准间隔的补充，单位为分钟
    Tbh = 8  # 保护液输送时间，单位：秒
    Tcx = 25  # 水冲洗时间，单位：秒
    flu_t = 30  # 支架冲洗时间，单位：秒
    atcal_t = 30  # 校准确认时间，单位：秒
    flu = 1  # 在线冲洗，0为没有，1为有
    atcal = 1  # 自动校准，0为没有，1为有
    tem_up_heat = 5  # 加热开启温度
    tem_down_heat = 10  # 加热关闭温度
    tem_up_fan = 50  # 风扇开启温度
    tem_down_fan = 48  # 风扇关闭温度
    hum_up_fan = 80  # 风扇开启湿度
    hum_down_fan = 75  # 风扇关闭湿度
    is_require_s14 = 0  # 从手动、自动保养切至自动清洗后，读取之前步骤九延时的时间
    s14_timing_judgement = 0  # 自动清洗退出后，判断是否要在手动中计时
    PH_start()
    time.sleep(2)
    query_mode()  # 查询当前所处模式
    try:
        can_open()
    except Exception as e:
        # 处理所有异常错误
        print("can error")
        print('Error:', e)
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^can通讯故障\")" % time1)  # 存入故障信息
        dox_command([20], err_inquiry_21_24)  # 输出do3
        error_20 = [20]
        pass
    try:
        dox_command([0], [0])
    except Exception as e:
        # 处理所有异常错误
        print("can error")
        print('Error:', e)
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^can通讯故障\")" % time1)  # 存入故障信息
        dox_command([20], err_inquiry_21_24)  # 输出do3
        error_20 = [20]
        pass
    if dox_command([0], [0]) is None:
        print("can error")
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^can通讯故障\")" % time1)  # 存入故障信息
        dox_command([20], err_inquiry_21_24)  # 输出do3
        error_20 = [20]
    serial_val_modify("homepage.now_status.txt=\"开启中\"")
    time.sleep(1)
    if PH_start() == 0:
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^PH仪表故障\")" % time1)  # 存入故障信息
        dox_command([20], err_inquiry_21_24)  # 输出do3
        error_20 = [20]
    else:
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        # serial_val_modify("warningpage.data0.insert(\"%s^PH仪表正常\")" % time1)  # 存入故障信息
    if insert_database() == "8888":
        time1 = time.strftime('%m-%d %H:%M:%S', time.localtime())
        serial_val_modify("warningpage.data0.insert(\"%s^切换阀故障\")" % time1)  # 存入故障信息
        dox_command([20], err_inquiry_21_24)  # 输出do3
        error_20 = [20]
    print(insert_database())
    time.sleep(1)
    dox_command(err_inquiry_all, [0])
    time.sleep(1)
    insert_error_num(0, 's3')
    insert_error_num(0, 's14')
    insert_error_num(0, 's15')
    sql_test("main_start")
    sql_test_del_main_restart()
    s14_switching = 0  # 预设机器为先校准运行模式
    try:
        s14_switching = sql_query_s14_switching()
    except Exception as e:
        # 处理所有异常错误
        print('s14_switching Error:', e)
        pass
    # 校准后运行为0，还是运行后校准为1
    if s14_switching == 1:
        serial_val_modify("more_1.switching.txt=\"先测量运行模式\"")
    else:
        serial_val_modify("more_1.switching.txt=\"先校准运行模式\"")
    serial_val_modify("warningpage.data0.insert(\"%s^设备重启完成\")" % time1)  # 存入故障信息
    while 1:
        for t in threads:
            t.start()
        for t in threads:
            t.join()
